Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paused audio resumes if app is suspended and then mic/speaker is released #148

Open
yringler opened this issue May 3, 2019 · 4 comments

Comments

@yringler
Copy link

yringler commented May 3, 2019

Problem

If user pauses audio
opens up other app
uses speaker or recorder from other app
when that app releases the speaker/recorder, audio in my app resumes.

Desired Behaviour
Release of speaker by another app should only resume playback if playback was paused by that app taking the speaker.

Possible work around
I'm going to try calling release resources when app is suspended. Update: Haven't gotten this to work.

@yringler yringler changed the title Paused audio replays if app is suspended and then mic/speaker is released Paused audio resumes if app is suspended and then mic/speaker is released May 3, 2019
@masayoshiadachi
Copy link

I have the same issue and I found out a workaround.
This issue is caused by regaining the audio focus after other app release the audio focus. When the audio player regain the audio focus, the "resume" method of player instance will be called. So suddenly paused audio start to play. You can see the issue with debug=true option.

My workaround is override the "resume" function.

    this.player = new TNSPlayer();
    // disable resume from getting focus
    this.player.resume = () => {
      console.log("do nothing");
    };

I use "play" and "pause" function from my app, so I don't need "resume" function.

@yringler
Copy link
Author

yringler commented May 8, 2019

Brilliant! Thank you so much. Tried your work around, it worked great.

yringler added a commit to yringler/rabbi-gordon-audio that referenced this issue May 8, 2019
@7ammer
Copy link

7ammer commented Jan 13, 2020

The resume function exists for a reason! :(

I think this issue has something to do with _mOnAudioFocusChangeListener method in the src/android/player.ts file.

I can see that after phoning someone and ending a call that case android.media.AudioManager.AUDIOFOCUS_GAIN: is switched which results in this.resume() getting triggered. What I think maybe going wrong is the user can pause the audio but AUDIOFOCUS_GAIN can still be triggered so it will start playing from a paused state!

code below...

  private _mOnAudioFocusChangeListener = new android.media.AudioManager.OnAudioFocusChangeListener({
    onAudioFocusChange: (focusChange: number) => {
      switch (focusChange) {
        case android.media.AudioManager.AUDIOFOCUS_GAIN:
          TNS_Player_Log('AUDIOFOCUS_GAIN');
.......
          this.resume();
          break;
.....

Perhaps the player state could be saved at AUDIOFOCUS_LOSS_TRANSIENT and then an IF statement could be used in AUDIOFOCUS_GAIN to decided if the player can resume() the audio or not.

@7ammer
Copy link

7ammer commented Jan 13, 2020

Or... remove the listener when the player is paused and add the listener again when it is playing. I think this may fix some similar issue with iOS as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants