添加播放列表自动循环

This commit is contained in:
zhw2590582 committed 2019-04-21 18:00:02 +08:00
1 parent 2ebdfb4a2d
commit 7fb29496fe
3 files changed
+92 -67

No files matched your search

+39 -28
View File
@@ -2,7 +2,7 @@ import './index.scss';
function artplayerPluginPlaylist(art) {
const {
utils: { append, errorHandle },
utils: { append, errorHandle, sleep },
} = art.constructor;
const {
player,
@@ -22,23 +22,33 @@ function artplayerPluginPlaylist(art) {
const $playlistInner = $playlist.querySelector('.artplay-playlist-inner');
let playlist = [];
art.on('afterAttachUrl', () => {
const index = playlist.findIndex(item => item.url === $video.src);
const $children = Array.from($playlistInner.children);
const $item = $children[index];
if (index !== -1 && $item) {
$children.forEach(item => item.classList.remove('active'));
$item.classList.add('active');
}
});
function switchUrl(index) {
const itemOption = playlist[index];
player.switchUrl(itemOption.url, itemOption.title).then(() => {
return player.switchUrl(itemOption.url, itemOption.title).then(() => {
art.emit('artplayerPluginPlaylist:change', itemOption);
});
}
function prevVideo() {
const index = playlist.findIndex(item => item.url === $video.src);
errorHandle(index !== -1, "Can't find Playlist item");
if (playlist.length === 1) {
return switchUrl(0);
}
const prevIndex = index - 1 === -1 ? playlist.length - 1 : index - 1;
return switchUrl(prevIndex);
}
function nextVideo() {
const index = playlist.findIndex(item => item.url === $video.src);
errorHandle(index !== -1, "Can't find Playlist item");
if (playlist.length === 1) {
return switchUrl(0);
}
const nextIndex = index + 1 === playlist.length ? 0 : index + 1;
return switchUrl(nextIndex);
}
proxy($playlistInner, 'click', e => {
const { optionIndex } = e.target.dataset;
if (optionIndex && Number(optionIndex) <= playlist.length - 1) {
@@ -52,6 +62,21 @@ function artplayerPluginPlaylist(art) {
}
});
art.on('video:ended', () => {
nextVideo().then(() => {
sleep(1000).then(player.play);
});
});
art.on('afterAttachUrl', () => {
const index = playlist.findIndex(item => item.url === $video.src);
const $children = Array.from($playlistInner.children);
const $item = $children[index];
if (index !== -1 && $item) {
$children.forEach(item => item.classList.remove('active'));
$item.classList.add('active');
}
});
return {
name: 'artplayerPluginPlaylist',
load(list) {
@@ -75,24 +100,10 @@ function artplayerPluginPlaylist(art) {
$playlist.style.display = 'none';
},
next() {
const index = playlist.findIndex(item => item.url === $video.src);
errorHandle(index !== -1, "Can't find Playlist item");
if (playlist.length === 1) {
switchUrl(0);
} else {
const nextIndex = index + 1 === playlist.length ? 0 : index + 1;
switchUrl(nextIndex);
}
nextVideo();
},
prev() {
const index = playlist.findIndex(item => item.url === $video.src);
errorHandle(index !== -1, "Can't find Playlist item");
if (playlist.length === 1) {
switchUrl(0);
} else {
const prevIndex = index - 1 === -1 ? playlist.length - 1 : index - 1;
switchUrl(prevIndex);
}
prevVideo();
},
};
}