这是录音与播放的一个js代码,两个项目文件运行一个可以,一个不可以
js文件
const recorderManager = wx.getRecorderManager();
const innerAudioContext = wx.createInnerAudioContext();
var init
Page({
data: {
time: 0, //录音时长
duration: 600000, //录音最大值ms 600000/10分钟
tempFilePath: "", //音频路径
},
/**开始录音 */
kaishi: function () {
clearInterval(init) //清除定时器
// 监听音频开始事件
recorderManager.onStart((res) => {
console.log('recorder start')
})
recorderManager.onStop((res) => {
console.log('recorder stop', res)
this.setData({
tempFilePath: res.tempFilePath,
})
this.recordingTimer(this.data.time)
})
const options = {
duration: this.data.duration,
sampleRate: 16000,
numberOfChannels: 1,
encodeBitRate: 96000,
format: 'mp3',
frameSize: 50,
}
this.recordingTimer()
recorderManager.start(options)
},
/**
* 暂停录音
*/
zanting: function () {
recorderManager.onPause(() => {
console.log('recorder pause')
})
this.recordingTimer(this.data.time)
recorderManager.pause()
},
/**
* 继续录音
*/
jixu: function () {
this.recordingTimer()
recorderManager.resume()
},
/**
* 停止录音
*/
tingzhi: function () {
recorderManager.onStop((res) => {
console.log('recorder stop', res)
this.setData({
tempFilePath: res.tempFilePath,
})
})
this.recordingTimer(this.data.time)
recorderManager.stop()
},
/**
* 播放录音
*/
bofang: function () {
//音频地址
innerAudioContext.src = this.data.tempFilePath
innerAudioContext.obeyMuteSwitch = false
innerAudioContext.play()
innerAudioContext.onEnded(() => {
innerAudioContext.stop()
})
},
//录音计时器
recordingTimer: function (time) {
var that = this
if (time == undefined) {
init = setInterval(function () {
var time = that.data.time + 1;
that.setData({
time: time
})
}, 1000);
} else {
clearInterval(init)
console.log("暂停计时")
}
},
})
总感觉问题处在本身项目上哪里冲突了,但是没有报错,查看不到具体哪里!当前页面没问题,我新建一个页面也时一样播放不出来。