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

sha*_*aka 6 react-native expo

我正在使用 expo 构建一个 React Native 应用程序。I\xe2\x80\x99m 使用 \xe2\x80\x98expo-av\xe2\x80\x99。

\n\n

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

\n\n

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

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

小智 7

一切都可以使用这样的参数:

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
});
Run Code Online (Sandbox Code Playgroud)

在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;
Run Code Online (Sandbox Code Playgroud)

2023 年更新:

对于 expo-av13.0使用:

await Audio.setAudioModeAsync({
    allowsRecordingIOS: false,
    staysActiveInBackground: true,
    interruptionModeIOS: InterruptionModeIOS.DuckOthers,
    playsInSilentModeIOS: true,
    shouldDuckAndroid: true,
    interruptionModeAndroid: InterruptionModeAndroid.DuckOthers,
    playThroughEarpieceAndroid: false
});
Run Code Online (Sandbox Code Playgroud)