采集语音是用mp3格式进行采集的,但是用安卓手机测试,语音就无法播放,我和我同事用多个安卓的真机测试都不能播, 但是ios就可以播。我在安卓上下载后台给我返回的链接,是可以进行打开和播放的,所以是uni.createInnerAudioContext()不能识别我的文件格式吗,我测试过mp3,m4a,aac等四种格式,mp3时会报第一张图的错,aac,m4a等其他格式时报第二张图的错,是我的文件格式有什么问题吗?
我的机型如下:
录音时的js
const recorderManager = uni.getRecorderManager();
// 开始录音
handleRecordStart(e) {
//longpress时触发
console.log("开始录音");
this.startPoint = e.touches[0];
this.recordText = "松开发送";
recorderManager.start({
format: "mp3",
}); //开始录音
uni.showToast({
title: "正在录音,上划取消发送",
icon: "none",
duration: 60000,
});
this.sendLock = false;
},
// 监听录音结束并发送语音
handleMonitorAudio() {
const self = this
recorderManager.onStop((res) => {
if (this.sendLock) {
//上锁不发送
} else {
//解锁发送,发送网络请求
if (res.duration < 1000)
uni.showToast({
title: "录音时间太短",
icon: "none",
duration: 1000,
});
else {
let {
tempFilePath,
duration
} = res;
uploadFile(tempFilePath).then((uploadRes) => {
duration = Math.ceil(duration / 1000);
let remoteUrl = this.$imgUrl + uploadRes;
if (remoteUrl && duration) {
self.postAudioToRongIm(
remoteUrl,
duration
// self.loadHistoryMessages
);
}
});
}
}
});
}
播放语音的js
<script>
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
export default {
components: {},
props: {
// 居左还是居右
position: {
type: String,
default: null,
},
// 音频链接
path: {
type: String,
default: null,
},
// 持续时间
duration: {
type: Number,
default: null,
},
},
data() {
return {
isPlaying: false,
audioContext: null,
};
},
mounted() {
console.log("mounted---this.isPlaying", this.isPlaying);
if (!this.audioContext) {
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
this.audioContext = innerAudioContext;
} else {
this.audioContext.destroy();
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
this.audioContext = innerAudioContext;
}
this.audioContext.onCanplay(() => {
console.log('音频进入可以播放状态,但不保证后面可以流畅播放');
})
this.audioContext.onPlay(() => {
this.isPlaying = true;
uni.showToast({
title: '正在播放录音',
icon: 'none',
duration: 60 * 1000
})
console.log("正在播放");
});
this.audioContext.onEnded(() => {
this.isPlaying = false;
uni.hideToast();
console.log("自然结束");
});
this.audioContext.onStop(() => {
this.isPlaying = false;
uni.hideToast();
console.log("停止播放");
});
this.audioContext.onError((res) => {
console.log('播放错误信息', res);
console.log(res.errMsg);
console.log(res.errCode);
});
},
beforeDestroy() {
this.audioStop();
this.audioContext.offPlay();
this.audioContext.offStop();
this.audioContext.offEnded();
this.audioContext.destroy();
},
methods: {
handlePlayAudio() {
let voicePath = this.path;
// TODO:是否会同时播放多个录音,待测试,如果是
// ,那么每次播放前先将全局其他的录音关闭,或者由父组件进行传递播放方法,在父组件中播放
if (voicePath) {
// console.log("this.isPlaying", this.isPlaying);
if (this.isPlaying) {
this.audioStop();
} else {
this.audioContext.src = voicePath;
console.log('当前播放音频', this.audioContext.src);
this.audioPlay();
}
}
},
audioPlay() {
console.log("进入了audioPlay");
this.audioContext.play();
},
audioStop() {
// console.log("进入了audioStop");
this.audioContext.stop();
},
},
};
</script>
qq2504038331
作者这个问题解决了吗
2023-05-19 09:49