概述
react-native 安卓端使用 react-native-sound长时间循环播放音频列表有时候会没声音。
下面是音频播放实现的代码如下:
import Sound from 'react-native-sound';
const Voices = {
allVoices: [],// 所有语音数组集合
callAndTranData: [],// 呼叫和输液信息
tempData: [],// 温湿度
tempCallAndTranData: [],
tempTempData: [],
newSound: null, //当前播放语音实例
currentIndex: 0, // 当前播放语音的下标
isFirstNum: 1, // 判断是否第一次播放语音
volume: 100, // 音量
nextPlayTimerId: null, // 播放下一个音频定时器id
getCountVoices(newList, oldList, tempList, type, key1, key2) { // newList: 刚进来的数据; oldList: 已在音频列表中存在的数据, (key1, key2): 用来比较的条件
let tempFlag = null, flag = null, list = [];
if (type === 'tempHum') { // 温湿度需要两个条件判断
newList.map(item1 => {
tempFlag = tempList.find(item2 => (item2.remindType === item1.remindType
&& item2[key1] === item1[key1] && item2[key2] === item1[key2]));
if (tempFlag) {
flag = oldList.find(item2 => (item2.remindType === item1.remindType
&& item2[key1] === item1[key1] && item2[key2] === item1[key2]));
flag && (list.push(flag));
} else {
list.push({ ...item1, playCount: 0, keyType: type });
}
});
} else {
newList.map(item1 => {
tempFlag = tempList.find(item2 => (item2.remindType === item1.remindType && item2[key1] === item1[key1]));
if (tempFlag) {
flag = oldList.find(item2 => (item2.remindType === item1.remindType && item2[key1] === item1[key1]));
flag && (list.push(flag));
} else {
list.push({ ...item1, playCount: 0, keyType: type });
}
});
}
return list;
},
setVoices(voices, type) {
if (this.allVoices.length === 0) {
this.isFirstNum = 1;
} else {
this.isFirstNum = 0;
}
switch (type) {
case 'callAndTran':
if (JSON.stringify(voices) != JSON.stringify(this.tempCallAndTranData)) {
this.callAndTranData = this.getCountVoices(voices, this.callAndTranData, this.tempCallAndTranData, 'callAndTran', 'uniqueId');
this.allVoices = [...this.callAndTranData, ...this.tempData, ...this.noiseData, ...this.powerData];
this.tempCallAndTranData = voices;
}
break;
case 'tempHum':
if (JSON.stringify(voices) != JSON.stringify(this.tempTempData)) {
this.tempData = this.getCountVoices(voices, this.tempData, this.tempTempData, 'tempHum', 'spaceType', 'type');
this.allVoices = [...this.tempData, ...this.callAndTranData, ...this.noiseData, ...this.powerData];
this.tempTempData = voices;
}
break;
default: break;
}
this.currentIndex = 0;
// 判断是否从头开播
(this.isFirstNum === 1 && this.allVoices.length > 0) && this.VoicePlay();
},
// 删除当前销毁的语音
reomveDisVoices(params, type) {
if (this.allVoices.length === 0) { // 所有语音数组集合长度为0, 重置
this.isFirstNum = 0;
this.newSound = null;
}
},
// 语音循环播报
VoicePlay() {
if (voiceFileUrl) {
if (this.newSound) { // 先释放资源
this.newSound.release();
this.newSound = null;
}
this.newSound = new Sound(voiceFileUrl, null, err => {
// 设置音量 (0 ~ 1)
this.newSound.setVolume(this.volume / 100);
if (!err) {
this.newSound.play(success => {
this.delayPlay();
});
} else {
this.delayPlay();
}
});
} else {
this.delayPlay();
// console.log('找不到音频文件!');
}
},
// 延迟2s
delayPlay() {
this.nextPlayTimerId && clearTimeout(this.nextPlayTimerId);
this.nextPlayTimerId = null;
!this.nextPlayTimerId && (this.nextPlayTimerId = setTimeout(() => {
if (this.currentIndex < this.allVoices.length - 1) {
this.currentIndex++;
} else {
this.currentIndex = 0;
}
this.voiceRelease();
}, 2000));
},
// 更改音量(0 ~ 100)
setVolume(num) {
if (num === this.volume) return;
this.volume = (num || num == 0) ? num : 100;
},
// 停止播报, 并释放资源
voiceStop() {
if (this.newSound) {
this.newSound.stop(() => {
this.newSound.release();
this.newSound = null;
});
}
},
// 释放资源, 播放下一个音频
voiceRelease() {
this.newSound && this.newSound.release();
this.newSound = null;
this.VoicePlay();
}
}
export default Voices;
最后
以上就是坚定月光为你收集整理的react-native 安卓端使用 react-native-sound播放有时候会没声音的全部内容,希望文章能够帮你解决react-native 安卓端使用 react-native-sound播放有时候会没声音所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复