簡體   English   中英

如何在Javascript / HTML5中將視頻播放列表隨機化

[英]How to randomize video playlist in Javascript / Html5

我是JS的初學者,我想知道如何隨機分配視頻播放列表(JS),我想使用畫布查看受“保護”的視頻(通過畫布傳遞視頻),然后將視頻循環到重新開始,視頻連續播放列表隨機化。 馬不停蹄

幫助將不勝感激! 提前致謝!!

您可以簡單地將列表生成為數組:

var videoList = [video1url, video2url, ...];

然后使用自定義分類器對其進行分類,該分類器將對其隨機化:

videoList.sort(function(a, b) {return 0.5 - Math.random()});

此處在線演示 (打開控制台以查看“已排序”列表)。

對於連續的視頻播放器,您可以查看我前一段時間做過的小提琴

var ctx = canvas.getContext('2d'),             /// get canvas
    w = canvas.width,                          /// cache dimensions
    h = canvas.height,
    video1 = document.createElement('video'),  /// two video elements
    video2 = document.createElement('video'),
    isVideo1 = false,                          /// toggler
    list = [],                                 /// video list
    current = 0,                               /// index
    isPlaying = false;                         /// is video playing

/// add event handlers so we know when a video finished
video1.addEventListener('ended', play, false);
video2.addEventListener('ended', play, false);

/**
 *  Then we add a few videos to our list
*/

/// first arg. is always mp4 version, second anything else (ogg, webm etc)
addVideo('somevideourl1.mp4', 'somevideourl.ogg');
addVideo('somevideourl2.mp4', 'somevideour2.webm');

/// here you re-sort your list with the random (see details for list)
list.sort(function(a, b) {return 0.5 - Math.random()});

/// get first video    
getVideo(list[0], play);

/// start loop
render();

/// this will start play the videos
function play(){

    /// what video to start
    var video = (isVideo1 === false ? video1 : video2),
        next = current;

    /// toggle    
    isVideo1 = !isVideo1;

    /// get next video in list
    next++;
    if (next > list.length - 1) next = 0;

    if (list.length > 0) getVideo(list[next]);

    /// start video
    video.play();
    isPlaying = true;

    current = next;
}

/// this draws the video frames to canvas    
function render() {
    if (isPlaying) {
        var video = (isVideo1 === true ? video1 : video2);
        ctx.drawImage(video, 0, 0, w, h);
    }
    requestAnimationFrame(render);
}

/// this function adds a video to the list    
function addVideo(urlMP4, url) {
    list.push({
        urlMP4: urlMP4,
        url: url,
        isReady: false
    })
}

/// start download a video
function getVideo(vid, callback) {

    var video = (isVideo1 === false ? video1 : video2),
        me = this;

    video.addEventListener('canplay', canPlay, false);;

    function canPlay(e) {
        video.removeEventListener('canplay', canPlay, false);
        vid.isReady = true;
        if (typeof callback === 'function')
            callback(vid);
    }

    if (video.canPlayType("video/mp4").length > 0) {
        video.src = vid.urlMP4;
    } else {
        video.src = vid.url;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM