<template>
<view class="content">
<view class="text-success">
<text>可以正常显示:</text>
<text>{{ successText }}</text>
</view>
<view class="text-error">
<text>错误显示(真机调试(IOS)显示-无法加载音频):</text>
<text>{{ errorText }}</text>
</view>
</view>
</template>
<script setup>
import { ref } from 'vue';
const getAudioDuration = (url)=> {
return new Promise((resolve) => {
const audioContext = uni.createInnerAudioContext();
audioContext.src = url;
const isError = ref(false);
audioContext.onError((res) => {
// TODO真机调试(IOS)会error
console.log('res', res);
const errMsg = (res.errMsg.match(/ERRMSG:(.+?)(?:,|$)/)?.[1] || '未知错误').trim();
isError.value = true;
uni.showToast({
icon: 'error',
title: errMsg,
});
});
const totalInterval = setInterval(() => {
const { duration } = audioContext;
if (duration != 0 && !Number.isNaN(duration)) {
console.log('duration',duration);
resolve(duration);
audioContext && audioContext.destroy();
clearInterval(totalInterval);
}
if (isError.value) {
resolve('无法加载音频');
clearInterval(totalInterval);
}
}, 100);
});
};
// success
const successText = ref('')
const handleSuccessAudioUrl= async() => {
successText.value = await getAudioDuration('https://gwsits.gdufs.edu.cn/oss/jvs-public/ten_1/2_2/jvs-auth-mgr/jvs-ui/file/2025/04/08/2025-04-081094227154607968256-%E6%B5%B7%E5%B0%94.m4a')
};
handleSuccessAudioUrl()
// error (微信开发者工具可以正常,用真机调试(IOS)就无法加载错误了 )
const errorText = ref('')
const handleErrorAudioUrl= async() => {
errorText.value = await getAudioDuration('https://gwsits.gdufs.edu.cn/oss/jvs-public/ten_1/2_2/jvs-auth-mgr/jvs-ui/file/2025/04/08/2025-04-081094227347868913664-IEA.m4a')
};
handleErrorAudioUrl()
</script>