update switchMix.js

This commit is contained in:
Harvey Zack committed 2023-05-04 12:40:31 +08:00
1 parent c301844a87
commit 37dc0a8203
7 files changed
+65 -62

No files matched your search

@@ -203,8 +203,6 @@ art.on('ready', () => {
设置视频地址,设置时和 `url` 类似,但会执行一些优化操作
函数支持两个参数,第一个为新的视频地址,第二个为可选新的视频名字
<div className="run-code">▶ Run Code</div>
```js{9}
@@ -216,7 +214,7 @@ var art = new Artplayer({
art.on('ready', () => {
art.seek = 10;
setTimeout(() => {
art.switchUrl('/assets/sample/video.mp4?t=0', 'New Video Name');
art.switchUrl('/assets/sample/video.mp4?t=0');
}, 3000);
});
```
@@ -228,8 +226,6 @@ art.on('ready', () => {
设置视频画质地址,和 `switchUrl` 类似,但会带上之前的播放进度
函数支持两个参数,第一个为新的视频地址,第二个为可选新的视频名字
<div className="run-code">▶ Run Code</div>
```js{9}
@@ -241,7 +237,7 @@ var art = new Artplayer({
art.on('ready', () => {
art.seek = 10;
setTimeout(() => {
art.switchQuality('/assets/sample/video.mp4?t=0', '新视频地址');
art.switchQuality('/assets/sample/video.mp4?t=0');
}, 3000);
});
```
@@ -203,8 +203,6 @@ art.on('ready', () => {
Setting the video url is similar to that of `url`, but some optimization operations will be performed
The function supports two parameters, the first is the new video url, and the second is the optional new video name
<div className="run-code">▶ Run Code</div>
```js{9}
@@ -216,7 +214,7 @@ var art = new Artplayer({
art.on('ready', () => {
art.seek = 10;
setTimeout(() => {
art.switchUrl('/assets/sample/video.mp4?t=0', 'New Video Name');
art.switchUrl('/assets/sample/video.mp4?t=0');
}, 3000);
});
```
@@ -228,8 +226,6 @@ art.on('ready', () => {
Set the video quality address, similar to `switchUrl`, but with the previous playback progress
The function supports two parameters, the first is the new video url, and the second is the optional new video name
<div className="run-code">▶ Run Code</div>
```js{9}
@@ -241,7 +237,7 @@ var art = new Artplayer({
art.on('ready', () => {
art.seek = 10;
setTimeout(() => {
art.switchQuality('/assets/sample/video.mp4?t=0', 'New Video Name');
art.switchQuality('/assets/sample/video.mp4?t=0');
}, 3000);
});
```
+22 -18
View File
@@ -1,42 +1,46 @@
import { def } from '../utils';
export default function switchMix(art) {
const { i18n, option, notice } = art;
function switchUrl(url, currentTime) {
return new Promise((resolve, reject) => {
if (url === art.url) return;
const { playing, aspectRatio, playbackRate, option } = art;
function switchUrl(url, name, currentTime) {
return new Promise((resolve) => {
if (url === art.url) return resolve(url);
const { playing } = art;
art.pause();
URL.revokeObjectURL(art.url);
art.url = url;
art.once('video:error', reject);
art.once('video:canplay', () => {
art.playbackRate = false;
art.aspectRatio = false;
art.flip = 'normal';
art.autoSize = option.autoSize;
art.playbackRate = playbackRate;
art.aspectRatio = aspectRatio;
art.currentTime = currentTime;
art.notice.show = '';
if (option.autoSize) {
art.autoSize = true;
}
if (playing) {
art.play();
}
if (name) {
notice.show = `${i18n.get('Switch Video')}: ${name}`;
}
resolve(url);
resolve();
});
});
}
def(art, 'switchQuality', {
value: (url, name) => {
return switchUrl(url, name, art.currentTime);
value: (url) => {
return switchUrl(url, art.currentTime);
},
});
def(art, 'switchUrl', {
value: (url, name) => {
return switchUrl(url, name, 0);
value: (url) => {
return switchUrl(url, 0);
},
});
def(art, 'switch', {
set: art.switchUrl,
});
}
+8 -7
View File
@@ -10,25 +10,26 @@ export default function urlMix(art) {
get() {
return $video.src;
},
async set(url) {
if (url) {
async set(newUrl) {
if (newUrl) {
const oldUrl = art.url;
const typeName = option.type || getExt(url);
const typeName = option.type || getExt(newUrl);
const typeCallback = option.customType[typeName];
if (typeName && typeCallback) {
await sleep();
art.loading.show = true;
typeCallback.call(art, $video, url, art);
typeCallback.call(art, $video, newUrl, art);
} else {
$video.src = url;
URL.revokeObjectURL(oldUrl);
$video.src = newUrl;
}
if (oldUrl !== art.url) {
art.option.url = url;
art.option.url = newUrl;
if (art.isReady && oldUrl) {
art.once('video:canplay', () => {
art.emit('restart', url);
art.emit('restart', newUrl);
});
}
}