我可以帮你解决前三个问题。
1、获取音频使用总时长,playbackStatus.durationMillis
获取当前音频使用时长playbackStatus.positionMillis
通过使用获取播放状态
export const createAudio = async () => {
const source = require('../../../assets/audio/storiesAudio/storyAudio.mp3');
const { sound } = await Audio.Sound.createAsync(source,
{
shouldPlay: false,
isLooping: false,
},
onPlaybackStatusUpdate,
);
this.setState({playbackInstance: sound});
}
onPlaybackStatusUpdate = async (playbackStatus) => {
await this.setState({ playbackStatus });
if (this.state.playbackStatus.isPlaying) {
}
}
将毫秒转换为分钟和秒
export const millisToMinutesAndSeconds = (millis) => {
let minutes = Math.floor(millis / 60000);
let seconds = ((millis % 60000) / 1000).toFixed(0);
return (seconds == 60 ? (minutes+1) + ":00" : minutes + ":" + (seconds < 10 ? "0" :
"") + seconds);
}
2、让音频在后台播放
const { sound } = await Audio.Sound.createAsync(
sourceAudioFile,
{
shouldPlay: true, //To play the audio when the component is loadded
isLooping: false,
},
onPlaybackStatusUpdate,
);
3,要检查当前音轨是否播放完毕,请使用playbackStatus.didJustFinish