whychdw
2021-08-02 8bd40db215402d4326679a3c67b3be9c530134bf
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
<template>
    <div class="rtmp-video-wrapper">
        <video ref="videoPlayer" :options="options" 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';
 
export default {
    name: "rtmpVideo",
    player: "",
    data() {
        return {
            options: {
                width: 800,
                height: 450,
                controls: true,        // 控制栏
                autoplay: false,         // 自动播放
                sources: [{             // 流配置,数组形式,会根据兼容顺序自动切换
                    type: 'rtmp/mp4',
                    src: 'rtmp://58.200.131.2:1935/livetv/hunantv' // 亲测可用
                }],
                techOrder: ['html5', 'flash'],
                flash: {
                    swf: SWF_URL
                }
            },
            playFlag: false,
        }
    },
    methods: {
        create() {
            let self = this;
            // 初始化播放器
            this.$options.player = videoJs(this.$refs.videoPlayer, this.options, function onPlayerReady() {
                console.log('onPlayerReady', this);
 
                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')
                })
            });
        },
        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();
        console.log(this.$options.player);
    },
    beforeDestroy() {
        this.dispose();
        console.log("销毁rtmp流");
    }
}
</script>
 
<style scoped>
.rtmp-video-wrapper {
    width: 100%;
    height: 100%;
}
</style>