125 lines
3.4 KiB
JavaScript
125 lines
3.4 KiB
JavaScript
/**
|
||
* Notes: APP语音播报
|
||
* @author WJD
|
||
* @version 1.0.0
|
||
* @Date 2022-01-26
|
||
*/
|
||
|
||
// 是否播放中
|
||
let isPlaying = false;
|
||
// 播放列表
|
||
let currentAudio = ['/static/audio/order.mp3'];
|
||
// 播放顺序
|
||
let playIndex = 0
|
||
// 播放器
|
||
let audioPlayer = null;
|
||
// 平台
|
||
let platform = 'iOS';
|
||
// 定时器
|
||
let timer = null;
|
||
|
||
|
||
const voice = {
|
||
// 创建播放器
|
||
init: () => {
|
||
platform = plus.os.name;
|
||
if (platform == 'Android') {
|
||
let MediaPlayer = plus.android.importClass("android.media.MediaPlayer");
|
||
audioPlayer = new MediaPlayer()
|
||
}
|
||
if (platform == "iOS") {
|
||
let AVAudioPlayer = plus.ios.importClass("AVAudioPlayer");
|
||
audioPlayer = new AVAudioPlayer();
|
||
}
|
||
console.log('创建播放器成功');
|
||
voice.playAudio();
|
||
},
|
||
|
||
// 重置播放列表
|
||
resetPlayList: () => {
|
||
playIndex = 0;
|
||
currentAudio = ['/static/audio/order.mp3'];
|
||
},
|
||
clearTimer: () => {
|
||
if (timer) {
|
||
clearInterval(timer);
|
||
timer = null;
|
||
}
|
||
|
||
},
|
||
// 播放音频
|
||
playAudio: () => {
|
||
|
||
if (isPlaying) {
|
||
console.log('播放中,请等待...')
|
||
return false;
|
||
}
|
||
if (audioPlayer == null) {
|
||
console.log('未启动音频播放器');
|
||
return false;
|
||
}
|
||
|
||
let audioList = currentAudio;
|
||
// voice.resetPlayList();
|
||
// if (currentAudio.length == 0) {
|
||
// const keys = Object.keys(audioList)
|
||
// console.log(audioList)
|
||
// if (keys.length) {
|
||
// currentAudio = Object.values(audioList)[0]
|
||
// delete audioList[keys[0]]
|
||
// playIndex = 0
|
||
// } else {
|
||
// voice.resetPlayList();
|
||
// }
|
||
// }
|
||
console.log('正在播报:', currentAudio[playIndex]);
|
||
let path = plus.io.convertLocalFileSystemURL(currentAudio[playIndex]);
|
||
let currentTime = 0; // 当前流媒体的播放的位置,单位是秒
|
||
let currentDuration = 0; // 当前文件时长
|
||
if (platform == 'iOS') {
|
||
let NSData = plus.ios.importClass("NSData");
|
||
let AVAudioPlayer = plus.ios.importClass("AVAudioPlayer");
|
||
let pathFileData = NSData.dataWithContentsOfFile(path);
|
||
audioPlayer.initWithDataerror(pathFileData, null);
|
||
// audioPlayer.setNumberOfLoops(-1); //-1:无限循环
|
||
audioPlayer.prepareToPlay(); //初始化播放器
|
||
audioPlayer.play();
|
||
isPlaying = audioPlayer.isPlaying();
|
||
// 当前播放进度 currentTime 秒
|
||
} else {
|
||
let MediaPlayer = plus.android.importClass("android.media.MediaPlayer");
|
||
audioPlayer.setDataSource(path); //指定音频文件路径
|
||
// audioPlayer.setLooping(true); //设置为循环播放
|
||
audioPlayer.prepare(); //初始化播放器MediaPlayer
|
||
audioPlayer.start();
|
||
isPlaying = audioPlayer.isPlaying();
|
||
// 获取音乐的总时长
|
||
console.log(isPlaying, audioPlayer.getDuration());
|
||
// 当前播放进度 getCurrentPosition 毫秒
|
||
}
|
||
|
||
timer = setInterval(function() {
|
||
currentTime = platform == 'iOS' ? audioPlayer.currentTime() : audioPlayer
|
||
.getCurrentPosition() / 1000;
|
||
currentDuration = platform == 'iOS' ? audioPlayer.duration() : audioPlayer
|
||
.getDuration() / 1000;
|
||
// console.log(currentTime, currentDuration)
|
||
if (currentTime == currentDuration || currentTime == 0) {
|
||
isPlaying = false;
|
||
voice.clearTimer();
|
||
if (playIndex < currentAudio.length - 1) {
|
||
playIndex++
|
||
} else {
|
||
currentAudio = [];
|
||
playIndex = 0;
|
||
}
|
||
if (Object.keys(audioList).length == 0 && currentAudio.length == 0) {
|
||
voice.resetPlayList();
|
||
}
|
||
// voice.playAudio();
|
||
}
|
||
}, 200);
|
||
}
|
||
}
|
||
|
||
export default voice |