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

Android: player plays last initialized sound after finishing a call #111

Closed
jnorkus opened this issue Jun 20, 2018 · 13 comments · Fixed by #188
Closed

Android: player plays last initialized sound after finishing a call #111

jnorkus opened this issue Jun 20, 2018 · 13 comments · Fixed by #188

Comments

@jnorkus
Copy link

jnorkus commented Jun 20, 2018

To reproduce, add this code, then receive and finish a phone call. The audio file is played immediately.

const player = new TNSPlayer();
player.initFromFile({
  audioFile: '~/audio/audio.mp3',
  loop: false
});
@jnorkus
Copy link
Author

jnorkus commented Jun 20, 2018

I found the cause of this bug. On Android every time AUDIOFOCUS_GAIN is fired on AudioManager, resume() is called even though the player was not playing when focus was lost.

@juliancesar
Copy link

juliancesar commented Jul 4, 2018

Did you get a workaround?

@jnorkus
Copy link
Author

jnorkus commented Jul 4, 2018

I did. I commented out the line and now use a local copy of the plugin. Quick and dirty but unfortunately no time to contribute.

@juliancesar
Copy link

@jnorkus, that line?

@NathanWalker, how can we improve this? Do you have any suggest?

@jnorkus
Copy link
Author

jnorkus commented Jul 4, 2018

Yes exactly that one.

@caiusCitiriga
Copy link

Any news on this guys?

@Robbt
Copy link

Robbt commented Aug 25, 2018

So I tested this with the this.resume() commented out as mentioned above and the audio still played and when I receive notifications it would play the sound. So I'm not sure what the fix is.

This is a pretty big bug though.

@nikolasdas
Copy link

Any news about this? :/

@bryanlilly
Copy link

bryanlilly commented Apr 24, 2019

My work around for this (in a local version of the plugin) was to make the following edits in ./nativescript-audio/android/player.js:

// Added to player.js line 11
this._resumeOnFocus = false;
// Added to player.js lines 28-31 
 if(_this._resumeOnFocus) {
     _this.resume();
      _this._resumeOnFocus = false;
}
// Added to player.js lines 39, 45, 50
if(_this.isAudioPlaying()) _this._resumeOnFocus = true;

Tried making the subsequent changes in the plugin itself, but after compiling and running the demo, i'm getting an extra AUDIOFOCUS_LOSS that repauses the player immediately after it gets focus switching back to the app after the phone call. Until I switch back the audio resumes or doesn't correctly, though.

@bryanlilly
Copy link

Otherwise, the above edits work in my actual app.

@Addeu
Copy link

Addeu commented May 6, 2019

I think I have an issue relative to this one. Player plays last initialized audio after colling and closing Google Assistant. This thing helped:
#111 (comment)

I did. I commented out the line and now use a local copy of the plugin. Quick and dirty but unfortunately no time to contribute.

@szgozcan
Copy link

I had exactly the same problem. Player plays last initializes audio even the app is in background when receives call or notification. #111 (comment) helped.

@Omar-Al-Ashi
Copy link

In my case, this answer fixed it 👍🏽

bradmartin pushed a commit that referenced this issue Mar 23, 2021
Previously audio focus was requested by the playFromFile method, which
is called during initFromFile and initFromUrl. Getting audio focus this
early means that when an AUDIOFOCUS_GAIN event is received, the track
will start playing even though the caller never requested it to play.

Now the audio focus is not requested until playing the track.

Fixes #111
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

Successfully merging a pull request may close this issue.

9 participants