使用循环初始化的播放器似乎存在一些问题。由于所有播放器都是同时创建和渲染的,因此音量控制仅适用于最后一个播放器实例。所以我在 amp 方法中使用了 onReady 函数,并使用了延迟一秒的递归。 (等待音量控制应用于每个播放器)。它对我有用。
let vPlayer = [];
const playOptions = { // Options
techOrder: ["azureHtml5JS", "flashSS", "html5FairPlayHLS", "silverlightSS", "html5"],
autoplay: false,
controls: true,
poster: "",
logo: { enabled: false },
};
const initPlayerInstances = () => {
let oldLength = vPlayer.length;
setEachPlayerViaRecursive(vPlayer.length, () => {
console.log(vPlayer);
setVideoSource(oldLength);
})
}
/**
* intialize each amp instance and wait for a second for it load in DOM
* and then go for the next operation. otherwise its mute and unmute are not working
* @param index intial index of vPlayer list
* @param callback when all vPlayers added go back and do next operations
*/
const setEachPlayerViaRecursive = (index, callback) => {
vPlayer[index] = amp('azuremediaplayer' + (index), playOptions, () => {
setTimeout(() => {
if (videoList.length - 1 > index) {
setEachPlayerViaRecursive(index + 1, callback);
} else {
callback()
}
}, 1000)
})
}
/**
* when all players initialized successfully, then set player urls
* for each vPlayer instance
*/
const setVideoSource = (oldLength) => {
vPlayer.forEach((player, i) => {
if (i > (oldLength - 1)) {
player.src([{
src: videoList[i].VideoUrl,
type: "application/vnd.ms-sstr+xml"
}]);
}
})
}
最后调用initPlayerInstances方法。
initPlayerInstances();