he wei
2025-06-06 895129470d7ee48183fc15b9ee18ef0880503e5d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<template>
  <div class="rtmp-video-wrapper">
    <video ref="videoPlayer" class="video-js"></video>
  </div>
</template>
 
<script>
import videoJs from "video.js";
import "videojs-flash";
import SWF_URL from "videojs-swf/dist/video-js.swf";
import loadImg from "@/assets/images/dw_bg.jpg";
 
export default {
  name: "rtmpVideo",
  player: "",
  props: {
    url: {
      type: String,
      default: "",
    },
  },
  data() {
    return {
      options: {
        width: 800,
        height: 450,
        controls: true, // 控制栏
        autoplay: false, // 自动播放
        sources: [
          {
            // 流配置,数组形式,会根据兼容顺序自动切换
            type: "rtmp/mp4",
            src: loadImg, // 亲测可用
          },
        ],
        techOrder: ["html5", "flash"],
        flash: {
          swf: SWF_URL,
        },
      },
      playFlag: false,
    };
  },
  watch: {
    url() {
      this.changeUrl();
    },
  },
  methods: {
    create() {
      let self = this;
      // 初始化播放器
      this.$options.player = videoJs(
        this.$refs.videoPlayer,
        this.options,
        function onPlayerReady() {
          this.on("pause", function () {
            console.log("暂停播放");
          });
          this.on("play", function () {
            self.playFlag = true;
            console.log("开始/恢复播放");
          });
          this.on("ended", function () {
            console.log("结束播放");
          });
          this.on("loadeddata", function () {
            console.log("loadeddata数据加载完成");
          });
          this.on("error", function () {
            console.log("error");
          });
        }
      );
    },
    changeUrl() {
      let url = this.url;
      if (this.$options.player) {
        let myPlayer = this.$options.player;
        myPlayer.src(url);
        myPlayer.load(url);
      }
    },
    play() {
      this.$nextTick(() => {
        if (this.$options.player) {
          this.$options.player.play();
        }
      });
    },
    dispose() {
      if (this.$options.player) {
        this.playFlag = false;
        this.$options.player.dispose();
        this.$options.player = "";
      }
    },
  },
  mounted() {
    this.create();
  },
  beforeDestroy() {
    this.dispose();
    console.log("销毁rtmp流");
  },
};
</script>
 
<style scoped>
.rtmp-video-wrapper {
  width: 100%;
  height: 100%;
}
</style>