leohxj
9/26/2013 - 7:41 AM

设置视频的缓冲时间

设置视频的缓冲时间

var isBufferTimeInterval = false,
            bufferTimeout,
            newTime = 0,
            oldTime = 0,
            bufferRange,
            isEnded,
            isBufferEnded,
            isPlaying = false;

        var isAnimateIn = false,
            isVideoIn = false;

        MediaElement('player1', {
            success: function(mediaElement) {

                mediaElement.addEventListener('play', function() {

                }, false);

                mediaElement.addEventListener('canplay', function(e) {
                    mediaElement.play();
                    isPlaying = true;

                    if (!isBufferTimeInterval) {
                        console.log('BufferInterval...');
                        isBufferTimeInterval = true;
                        bufferTimeout = setInterval(function() {
                            bufferRange = mediaElement.buffered.end(0) - mediaElement.currentTime,
                            isEnded = mediaElement.currentTime == mediaElement.duration,
                            isBufferEnded = Math.abs(mediaElement.buffered.end(0) - mediaElement.duration) < 0.1;
                            console.log('-------------info-------------');
                            console.log('isPlaying: ', isPlaying);
                            console.log('durationTime: ', mediaElement.duration);
                            console.log('currentTime: ', mediaElement.currentTime);
                            console.log('buffered: ', mediaElement.buffered.end(0));
                            console.log('bufferRange: ', bufferRange);
                            console.log('isEnded: ', isEnded);
                            console.log('isBufferEnded: ', isBufferEnded);

                            if (isEnded) {
                                clearInterval(bufferTimeout);
                            }

                            if (isPlaying) {
                                // is playing
                                oldTime = newTime;
                                newTime = mediaElement.currentTime;
                                if ((newTime- oldTime) < 0.4) { // 0.4是判断是否停止的时间
                                    mediaElement.pause();
                                    isPlaying = false;
                                }
                            } else {
                                // is paused
                                if (!isEnded && Math.abs(bufferRange > 5) || isBufferEnded) {
                                    mediaElement.play();
                                    isPlaying = true;
                                }
                            }
                        }, 500); // 设置后台刷帧监听时间间隔
                    }
                }, false);

                mediaElement.addEventListener('pause', function(e) {

                }, false);

                mediaElement.addEventListener('playing', function(e) {
                    $('.video-loading, .video-bg').css('opacity', '0');
                }, false);
                mediaElement.addEventListener('ended', function(e) {

                }, false);

                mediaElement.addEventListener('timeupdate', function() {

                }, false);

            }
        });