gpt4 book ai didi

react-native - 如何使用 expo-av 在 React Native 中播放背景音频?

转载 作者:行者123 更新时间:2023-12-04 15:28:58 36 4
gpt4 key购买 nike

我正在使用 expo 构建一个 React Native 应用程序。我正在使用“expo-av”。

我在后台播放音频文件时遇到问题。

我有以下内容,但当我将我的应用程序切换到另一个应用程序时仍然无法让它工作。我没有收到任何错误;音乐只是在我的应用程序中停止。关于如何让它发挥作用的任何建议?或者我的代码还有其他地方可以关闭吗?

  async componentDidMount() {
try {
await Audio.setAudioModeAsync({
allowsRecordingIOS: false,
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_MIX_WITH_OTHERS,
// interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DUCK_OTHERS,
playsInSilentModeIOS: true,
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
shouldDuckAndroid: true,
staysActiveInBackground: true,
playThroughEarpieceAndroid: true
})
this.loadAudio();
// this.interval = setInterval(() => this.getStatus(), 1000);
} catch (e) {
console.log(e)
}
}

最佳答案

一切都适用于这样的参数:

Audio.setAudioModeAsync({
allowsRecordingIOS: false,
staysActiveInBackground: true,
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DUCK_OTHERS,
playsInSilentModeIOS: true,
shouldDuckAndroid: true,
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
playThroughEarpieceAndroid: false
});

在 package.json 中
"expo-av": "^6.0.0",

在播放器中:
componentDidMount() {
Audio.setAudioModeAsync({
allowsRecordingIOS: false,
staysActiveInBackground: true,
interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DUCK_OTHERS,
playsInSilentModeIOS: true,
shouldDuckAndroid: true,
interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
playThroughEarpieceAndroid: false
});
this._loadNewPlaybackInstance(true);
}

async _loadNewPlaybackInstance(playing) {
if (this.playbackInstance != null) {
await this.playbackInstance.unloadAsync();
this.playbackInstance = null;
}
const source = { uri: 'http://music.ru/music.mp3' };

const initialStatus = {
shouldPlay: playing,
rate: this.state.rate,
shouldCorrectPitch: this.state.shouldCorrectPitch,
volume: this.state.volume,
isMuted: this.state.muted,
isLooping: this.state.loopingType === LOOPING_TYPE_ONE
// // UNCOMMENT THIS TO TEST THE OLD androidImplementation:
// androidImplementation: 'MediaPlayer',
};
const {sound, status} = await Audio.Sound.createAsync(
source,
initialStatus,
this._onPlaybackStatusUpdate
);
this.playbackInstance = sound;

关于react-native - 如何使用 expo-av 在 React Native 中播放背景音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61671304/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com