expo: Audio stops playing after while with expo-av

🐛 Bug Report

Environment

Android

Expo CLI 3.17.15 environment info: System: OS: Windows 10 10.0.18363 Binaries: Node: 12.16.1 - S:\nodejs\node.EXE Yarn: 1.17.3 - C:\Users\PooyA\AppData\Roaming\npm\yarn.CMD npm: 6.13.4 - S:\nodejs\npm.CMD

Steps to Reproduce

when playing a music using expo-av components. after running the app, music plays correctly but after while,unreasonable its stops playing just before the music ends in random duration in each time running the app!!

Expected Behavior

Play music without problems Or even show some errors!

Actual Behavior

stopping music after random while (!!) without any error just this warning:

Setting a timer for a long period of time, i.e. multiple minutes, is a performance and correctness issue on Android as it keeps the timer module awake, and timers can only be called when the app is in the foreground.

Simplified Codes

import React, { Component } from 'react'
import { Audio } from 'expo-av'

handlePlaySound = async fileAdd => {
    const soundObj = new Audio.Sound();
    try{
        await soundObj.loadAsync(fielAdd);
        await soundObj.playAsync;
    }catch(error){
        console.log(error);
    }
}
export default function HomeScreen() {
    let source = require('./assets/sounds/bg.mp3');
    this.handlePlaySound(source);

    return (.....Ui Components
    );
  }

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 25 (4 by maintainers)

Most upvoted comments

I ended up “fixing” my problem by using unloadAsync within the setOnPlaybackStatusUpdate callback ->

const soundObject = new Audio.Sound();
try {
    soundObject.setOnPlaybackStatusUpdate((status) => {
        if (!status.didJustFinish) return;
        soundObject.unloadAsync();
    });
    const audioFile = audioMap[fileName.replace(/ /g, '')];
    await soundObject.loadAsync(audioFile);
    await soundObject.playAsync();
} catch (error) {
    console.log(error);
}

However, I believe my problem is more related to constant stopping and starting of new, short audio clips rather than prolonged duration of one.

It’s still there in sdk 49…sigh!!

This issue still exists and should be reopened on Expo v44 expo-av: ~10.2.0

@krschacht Yes, it should be compatible with expo. There is no eject anymore in the latest expo versions 🤷🏻

Anyway, we ended up using react-native-video instead of react-native-sound, because it supports playing both video and audio files. Does not support recording though.

If you decide to go for react-native-video, I would suggest you to go for the alpha version because it’s actually pretty much stable, unless you want to use subtitles (still work in progress)

I’m not one for posting “us too!”, but this has been the case for about a year. We’ve tried all manners of workarounds. In the past I think we got it working by calling an internal method of AV that allowed you to destroy objects. Now we destroy the whole screen when possible.

Playing multiple, short-duration audios in succession brings up the issue 9 times out of 10. Mostly on Android. We do unload the audios after finishing, and we did follow all suggestions in this and other threads.

Very standard issue management is at work here: multiple people report a problem, but since it can’t be easily reproduced, no one cares and the issues get closed. Since audio playback is the bread and butter of our app, ejecting from Expo is the only option.

We tremendously value the safety of Expo compared to the Wild West of RN, but issues with AV, SecureStore, Auth, etc., all that get closed without a fix, are not compatible with the needs of professional apps 🤷

For me it was just a memory leak. U have to unmount it

  React.useEffect(() => {
    return sound
      ? () => {
          console.log('Unloading Sound');
          sound.unloadAsync();
        }
      : undefined;
  }, [sound]);

I’m seeing a similar issue with expo av. Audio stops with this conditions:

  • Android device (iOS works fine)
  • App in background or locked for a random while.
  • Audio goes back when in foreground.

The only difference in my case is that it is a radio stream, there is no file to load and no end, so not sure if the fix applies the same. I’m using unloadAsync just when unmounting the component.

More details:

  • expo cli 3.26.0
  • expo 38.0.9
  • expo-av 8.2.1

Android models tested:

  • ZTE, Android 9
  • Huawei, Android 10

Any recommendation?

*** UPDATE Sep-3 *** New tests confirm that this related issue is only seen when the Android device is locked and not charging. It seems exoplayer playing in background consumes enough battery to make the device battery optimization stop the playback. Does this behavior match what others are seeing? otherwise I may need to open a new issue to track power management & expo-av in Android.

This is also happening to me! Mine involves playing a bunch of different sounds and then after a while it just stops all audio… I’ll see if I can get logs around whats going on when it fails, but it’s reproduceable on my published app on Google Play.