|
@@ -36,14 +36,22 @@ async function initDefaultAudio() {
|
|
|
immediate: true,
|
|
|
})
|
|
|
|
|
|
- watch(currentAudio, (val) => {
|
|
|
-
|
|
|
+ watch(currentAudio, (val, old) => {
|
|
|
+ const isSame = unref(old) && unref(val).url === unref(old).url;
|
|
|
+ console.log('isSame', isSame);
|
|
|
if (unref(val) && unref(val).url.length > 0) {
|
|
|
- console.log('currentAudio', unref(val).url)
|
|
|
if (unref(currentPlayer)) {
|
|
|
- unref(currentPlayer).changeUrl(unref(val).url);
|
|
|
+ const url = unref(val).url
|
|
|
+ const autoplay = unref(val).isAuto
|
|
|
+ const loop = unref(val).repeat
|
|
|
+ console.log('currentAudio', unref(val).url, autoplay, loop);
|
|
|
+ if (!isSame) {
|
|
|
+ unref(currentPlayer).changeUrl(url, autoplay, loop);
|
|
|
+ } else {
|
|
|
+ //相同URL的再次播放
|
|
|
+ unref(currentPlayer).resume();
|
|
|
+ }
|
|
|
}
|
|
|
- // currentPlayer.value.changeUrl(unref(val).url);
|
|
|
}
|
|
|
}, {
|
|
|
deep: true,
|
|
@@ -54,7 +62,7 @@ function initAudioPlayer() {
|
|
|
if (!unref(isInit)) {
|
|
|
isInit.value = true
|
|
|
console.log('initAudioPlayer');
|
|
|
- const player = createAudioPlayer(unref(currentAudio).url);
|
|
|
+ const player = createAudioPlayer(unref(currentAudio).url, unref(currentAudio).isAuto, unref(currentAudio).repeat);
|
|
|
currentPlayer.value = player
|
|
|
player.on('play', () => {
|
|
|
console.log('play--22', player.isPlaying);
|
|
@@ -98,10 +106,11 @@ function watchResetV4BGM(data) {
|
|
|
|
|
|
}
|
|
|
|
|
|
-function createAudioPlayer(url) {
|
|
|
+function createAudioPlayer(url, autoplay, loop = true) {
|
|
|
const player = new AudioPlayer({
|
|
|
src: url,
|
|
|
- loop: true
|
|
|
+ autoplay: autoplay,
|
|
|
+ loop: loop
|
|
|
});
|
|
|
|
|
|
return player
|
|
@@ -111,6 +120,7 @@ class AudioPlayer {
|
|
|
constructor(options) {
|
|
|
this._src = options.src;
|
|
|
this._loop = options.loop;
|
|
|
+ this._autoplay = options.autoplay;
|
|
|
this._isPlaying = false;
|
|
|
const emitter = mitt()
|
|
|
Object.keys(emitter).forEach((method) => {
|
|
@@ -122,10 +132,12 @@ class AudioPlayer {
|
|
|
get isPlaying() {
|
|
|
return this._isPlaying;
|
|
|
}
|
|
|
- changeUrl(url) {
|
|
|
- console.log('changeUrl');
|
|
|
+ changeUrl(url, autoplay, loop) {
|
|
|
+ // console.log('changeUrl', arguments);
|
|
|
this.audio.unload();
|
|
|
this._isPlaying = false;
|
|
|
+ this._autoplay = autoplay || false;
|
|
|
+ this._loop = loop || false;
|
|
|
this._src = url;
|
|
|
this.audio = null;
|
|
|
this.createAudio();
|
|
@@ -179,10 +191,11 @@ class AudioPlayer {
|
|
|
async play() {
|
|
|
try {
|
|
|
if (!this._isPlaying) {
|
|
|
- console.log('play--1');
|
|
|
- await this.audio.play();
|
|
|
+ console.log('play---1', this._autoplay)
|
|
|
+ if (this._autoplay) {
|
|
|
+ await this.audio.play();
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
} catch (error) {
|
|
|
console.warn('playError', error);
|
|
|
}
|
|
@@ -190,6 +203,9 @@ class AudioPlayer {
|
|
|
pause() {
|
|
|
return this.audio.pause();
|
|
|
}
|
|
|
+ resume() {
|
|
|
+ return this.audio.play();
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
export function useAudio() {
|