18045010223
7 天以前 afe371d39a054b2f2a9e5875b945584eec8a8141
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<html>
<head>
    <title>HTTP-Flv Test</title>
</head>
<!--<script src="//cdn.bootcss.com/jquery/3.4.1/jquery.min.js" type="text/javascript"></script>
<script src="//cdn.bootcss.com/flv.js/1.5.0/flv.min.js" type="text/javascript"></script>-->
 
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/flv.js/1.5.0/flv.js" integrity="sha512-3o5c2VekAg9ZZgcmddbSufUCJjqqY7uypQUa2JeCCgLjVKA1KOfqrMMGwbgP9tCszbZXhG7agevYBh2sm3I0JA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<body style="background-color: #666666">
<div id="xxoo" style="background-color: #333333;border-radius: 10px; overflow: hidden; width: 400px; height: 300px;"></div>
<input id="tag">
<script type="text/javascript">
/**
 * 创建一个FLV播放器,参数如下:
 * {
 *      container : 视频容器元素
 *      muted     : 是否静音
 *      url       : HTTP-FLV地址
 * }
 */
function FLVPlayer(opts)
{
    var videoElement = document.createElement('VIDEO');
    videoElement.autoplay = true;
    videoElement.controls = false;
    videoElement.muted = false;
    videoElement.style.width = '100%';
    videoElement.style.height = '100%';
    opts.container.append(videoElement);
 
    this.container = opts.container;
    this.videoElement = videoElement;
    this.httpFlvURL = opts.url;
 
    this.mediaInfo = null;
    this.play = null;
    this.onPlayEvtListener = null;
    this.onPauseEvtListener = null;
    this.onStopEvtListener = null;
 
    this.autoFastForward = opts.autoFastForward;
    this.autoFastForwardInterval = null;
 
    this.play = function()
    {
        if (this.player) return;
 
        var self = this;
        self.player = new flvjs.createPlayer({
            type                            : 'flv',
            url                             : self.httpFlvURL,
            isLive                          : true,
            enableWorker                    : true,
            enableStashBuffer               : true,
            autoCleanupSourceBuffer         : true,
            autoCleanupMaxBackwardDuration  : 5,
            autoCleanupMinBackwardDuration  : 1
        });
 
        self.player.on('media_info', function()
        {
            self.mediaInfo = self.player.mediaInfo;
        });
 
        self.player.on('statistics_info', function()
        {
            console.log(arguments);
        });
 
        var autoPlayTimer = null;
        self.videoElement.addEventListener('player', function(e)
        {
            if (autoPlayTimer) clearInterval(autoPlayTimer);
            if (self.onPlayEvtListener) self.onPlayEvtListener(self, e);
        });
        self.videoElement.addEventListener('dblclick', function()
        {
            if (self.videoElement.requestFullscreen) self.videoElement.requestFullscreen();
        });
        autoPlayTimer = setInterval(function()
        {
            try { self.player.play(); } catch(e) { clearInterval(autoPlayTimer); };
        });
 
        self.player.attachMediaElement(self.videoElement);
        self.player.load();
        self.player.play();
 
        if (this.autoFastForward) this.autoFastForwardInterval = setInterval(function()
        {
            if (self.videoElement.buffered.length > 0 && self.videoElement.buffered.end(0) - self.videoElement.currentTime > 2)
            {
                console.log(self.videoElement.buffered.end(0) + "-" + self.videoElement.currentTime);
                self.videoElement.currentTime = self.videoElement.buffered.end(0) - 1;
            }
        }, 1000);
    };
 
    this.fullscreen = function()
    {
        if (this.videoElement && this.videoElement.requestFullscreen)
            this.videoElement.requestFullscreen();
    };
 
    this.onPlay = function(fn)
    {
        this.onPlayEvtListener = fn;
    };
 
    this.destroy = function()
    {
        this.player.destroy();
        clearInterval(this.autoFastForwardInterval);
    }
}
</script>
<script type="text/javascript">
    if (location.hash)
    {
        var hash = location.hash.substring(1);
        $('#tag').val(hash);
    }
    function playVideo()
    {
        var videoPlayer = new FLVPlayer({
            container : $('#xxoo'),
            url : '/video/' + $('#tag').val(),
            // 自动快进追祯,但是可能会导致画面停顿
            autoFastForward : false
        });
        videoPlayer.play();
    }
</script>
<button onclick="playVideo()">Play Video</button>
</body>
</html>