|
@@ -9,14 +9,17 @@ export function useSimpleAudioPlayer(config: {
|
|
|
const timeSec = ref(0);
|
|
|
const timeString = ref('00:00');
|
|
|
const isPlaying = ref(false);
|
|
|
+ const isEnded = ref(false);
|
|
|
const loadedState = ref(false);
|
|
|
const innerAudioContext = uni.createInnerAudioContext();
|
|
|
+ let loading = false
|
|
|
|
|
|
function load(src: string) {
|
|
|
- if (isPlaying.value)
|
|
|
- pause();
|
|
|
+ loading = true;
|
|
|
loadedState.value = true;
|
|
|
- innerAudioContext.src = src;
|
|
|
+ innerAudioContext.stop();
|
|
|
+ if (innerAudioContext.src != src)
|
|
|
+ innerAudioContext.src = src;
|
|
|
innerAudioContext.play();
|
|
|
timeString.value = '加载中';
|
|
|
}
|
|
@@ -27,6 +30,10 @@ export function useSimpleAudioPlayer(config: {
|
|
|
function pause() {
|
|
|
innerAudioContext.pause();
|
|
|
}
|
|
|
+ function restart() {
|
|
|
+ innerAudioContext.seek(0);
|
|
|
+ innerAudioContext.play();
|
|
|
+ }
|
|
|
function playpause() {
|
|
|
if (isPlaying.value) {
|
|
|
innerAudioContext.pause();
|
|
@@ -38,46 +45,50 @@ export function useSimpleAudioPlayer(config: {
|
|
|
function seek(sec: number) {
|
|
|
innerAudioContext.seek(sec);
|
|
|
}
|
|
|
+ function stop() {
|
|
|
+ innerAudioContext.stop();
|
|
|
+ }
|
|
|
|
|
|
innerAudioContext.onEnded(() => {
|
|
|
isPlaying.value = false;
|
|
|
+ isEnded.value = true;
|
|
|
+ if (loading)
|
|
|
+ return;
|
|
|
if (config.onEnded)
|
|
|
config.onEnded();
|
|
|
});
|
|
|
innerAudioContext.onPlay(() => {
|
|
|
+ loading = false;
|
|
|
isPlaying.value = true;
|
|
|
+ isEnded.value = false;
|
|
|
});
|
|
|
innerAudioContext.onPause(() => {
|
|
|
isPlaying.value = false;
|
|
|
+ isEnded.value = false;
|
|
|
});
|
|
|
innerAudioContext.onSeeking(() => {
|
|
|
+ loading = true;
|
|
|
timeString.value = '加载中';
|
|
|
});
|
|
|
- innerAudioContext.onSeeking(() => {
|
|
|
- timeString.value = '加载中';
|
|
|
+ innerAudioContext.onSeeked(() => {
|
|
|
+ loading = false;
|
|
|
});
|
|
|
innerAudioContext.onWaiting(() => {
|
|
|
+ loading = true;
|
|
|
timeString.value = '加载中';
|
|
|
});
|
|
|
innerAudioContext.onError((err) => {
|
|
|
console.error(err);
|
|
|
timeString.value = '错误';
|
|
|
- toast('播放音频失败');
|
|
|
- isPlaying.value = false;
|
|
|
- });
|
|
|
- innerAudioContext.onError((err) => {
|
|
|
- console.error(err);
|
|
|
- toast('播放音频失败');
|
|
|
isPlaying.value = false;
|
|
|
+ isEnded.value = false;
|
|
|
+ loading = false;
|
|
|
});
|
|
|
innerAudioContext.onTimeUpdate(() => {
|
|
|
duration.value = innerAudioContext.duration;
|
|
|
timeSec.value = innerAudioContext.currentTime;
|
|
|
timeString.value = TimeUtils.getTimeStringSec(timeSec.value) + '/' + TimeUtils.getTimeStringSec(duration.value);
|
|
|
});
|
|
|
- innerAudioContext.onEnded(() => {
|
|
|
- isPlaying.value = false;
|
|
|
- });
|
|
|
onUnmounted(() => {
|
|
|
innerAudioContext.destroy();
|
|
|
});
|
|
@@ -87,11 +98,14 @@ export function useSimpleAudioPlayer(config: {
|
|
|
timeString,
|
|
|
loadedState,
|
|
|
isPlaying,
|
|
|
+ isEnded,
|
|
|
duration,
|
|
|
seek,
|
|
|
load,
|
|
|
+ stop,
|
|
|
play,
|
|
|
playpause,
|
|
|
+ restart,
|
|
|
pause,
|
|
|
};
|
|
|
}
|
|
@@ -104,9 +118,11 @@ export interface SimpleListAudioPlayerItem {
|
|
|
export function useSimpleListAudioPlayer<T extends SimpleListAudioPlayerItem>(
|
|
|
loadList: () => Promise<T[]>,
|
|
|
autoNext: boolean = false,
|
|
|
+ onEnded?: () => void,
|
|
|
) {
|
|
|
const player = useSimpleAudioPlayer({
|
|
|
onEnded: () => {
|
|
|
+ onEnded?.();
|
|
|
if (autoNext) {
|
|
|
next();
|
|
|
}
|
|
@@ -125,10 +141,14 @@ export function useSimpleListAudioPlayer<T extends SimpleListAudioPlayerItem>(
|
|
|
return list.value[currentIndex.value] || null;
|
|
|
})
|
|
|
|
|
|
- onMounted(async () => {
|
|
|
- list.value = await loadList();
|
|
|
+ onMounted(() => {
|
|
|
+ reloadList();
|
|
|
})
|
|
|
|
|
|
+ async function reloadList() {
|
|
|
+ list.value = await loadList();
|
|
|
+ }
|
|
|
+
|
|
|
function loadToPlayer() {
|
|
|
player.load(list.value[currentIndex.value].src);
|
|
|
}
|
|
@@ -168,5 +188,6 @@ export function useSimpleListAudioPlayer<T extends SimpleListAudioPlayerItem>(
|
|
|
currentItem,
|
|
|
next,
|
|
|
prev,
|
|
|
+ reloadList,
|
|
|
}
|
|
|
}
|