expo / expo

An open-source framework for making universal native apps with React. Expo runs on Android, iOS, and the web.
https://docs.expo.dev
MIT License
32.83k stars 5.22k forks source link

[AV] Playback stopped by Android when device locked and not charging #10056

Closed gianpietro-magiqapps closed 2 years ago

gianpietro-magiqapps commented 4 years ago

🐛 Bug Report

Summary of Issue

In Android devices with latest expo-av (Exoplayer), if the device is locked and not charging, it seems that some power management feature literally "stops" the playback after playing for a while, until the app is opened again. This does not happen with iOS, nor in Android with other music playing apps subject to the same power management features (Spotify, TuneIn, etc)

Environment - output of expo diagnostics & the platform(s) you're targeting

Using expo-av 8.6.0 under the following environment:

  Expo CLI 3.21.13 environment info:
    System:
      OS: macOS 10.15.2
      Shell: 5.7.1 - /bin/zsh
    Binaries:
      Node: 12.18.1 - /usr/local/bin/node
      Yarn: 1.22.4 - /usr/local/bin/yarn
      npm: 6.14.5 - /usr/local/bin/npm
    IDEs:
      Android Studio: 4.0 AI-193.6911.18.40.6514223
      Xcode: 11.5/11E608c - /usr/bin/xcodebuild
    npmPackages:
      expo: ~38.0.8 => 38.0.9
      react: ~16.11.0 => 16.11.0
      react-dom: ~16.11.0 => 16.11.0
      react-native: https://github.com/expo/react-native/archive/sdk-38.0.2.tar.gz => 0.62.2
    npmGlobalPackages:
      expo-cli: 3.21.13

Reproducible Demo

Here's some sample code I'm using for my tests:

const initAudio = async () => {
    await Audio.setAudioModeAsync({
      allowsRecordingIOS: false,
      interruptionModeIOS: Audio.INTERRUPTION_MODE_IOS_DUCK_OTHERS,
      playsInSilentModeIOS: true,
      interruptionModeAndroid: Audio.INTERRUPTION_MODE_ANDROID_DUCK_OTHERS,
      shouldDuckAndroid: true,
      staysActiveInBackground: true,
      playThroughEarpieceAndroid: false,
    });

    let localPlaybackInstance = new Audio.Sound();
    let source = {
      uri: `https://streamer.radio.co/${activeRadioId}/listen`,
    };

    const status = {
      shouldPlay: isPlaying,
      volume: volume,
      androidImplementation: "MediaPlayer",
    };

    const onPlaybackStatusUpdate = async (status) => {
      setIsBuffering(status.isBuffering);
    };

    localPlaybackInstance.setOnPlaybackStatusUpdate(onPlaybackStatusUpdate);
    await localPlaybackInstance.loadAsync(source, status, false);
    await addPlaybackInstance(localPlaybackInstance);
  };

Steps to Reproduce

Expected Behavior vs Actual Behavior

Just like in iOS or in other Android media playing apps, Android devices should let the expo-av playback continue in background even without needing to plug the charger.

IjzerenHein commented 4 years ago

literally "stops" the playback after playing for a while what exactly do you mean after a while? Can you give a ball park figure?

Also, did the app get terminated by Android entirely (which might happen if Android thinks the app is using too much cpu/mem), or did it only stop the audio?

gianpietro-magiqapps commented 4 years ago

Hi @IjzerenHein, this happens after 10-15min of the phone being locked and not charging.

The app is not terminated, it's just the audio that stops. When we visit back the app, audio restarts normally. We have also noticed that the app appears as "stopped" in the "battery usage" view (attaching examples)

App running: appRunning

After 10-15 minutes of phone not charging: appStopped

chromacoma commented 3 years ago

I can confirm that a similar thing is happening when recording. I've opened a separate issue here: #10501

gianpietro-magiqapps commented 3 years ago

Hi, I had to eject and use react-native-track-player, which Android and iOS do recognize as an audio player, so it got rid of the issue and also shows player information in the native widgets.

garrettg123 commented 2 years ago

Can confirm this is stil happening on Expo v43 and v44 expo-av: ~10.2.0

github-actions[bot] commented 2 years ago

This issue is stale because it has been open for 60 days with no activity. If there is no activity in the next 7 days, the issue will be closed.

github-actions[bot] commented 2 years ago

This issue was closed because it has been inactive for 7 days since being marked as stale. Please open a new issue if you believe you are encountering a related problem.

connorlindsey commented 2 years ago

I'm currently experiencing this on iOS as well, even with different setAudioModeAsync settings like staysActiveInBackground. Would love some input from the Expo team on this.

adamtsai1 commented 1 year ago

I am experiencing this issue as well (on Android). Expo 44, expo-av 10.2.0.

aelxxs commented 1 year ago

I'm also experiencing this issue. The audio stops after a minute or so. I'll be switching to react-native-track-player.