I want to stop media player when some other media player applications start.
I don't know how to do this.
You'll need to add listener for AudioFocusChange.
Refer: https://developer.android.com/reference/android/media/AudioManager.OnAudioFocusChangeListener.html
I've done the similar thing in a project where when my app starts playing, Google Play Music pause and vice versa.
AudioManager am = null;
// Request focus for music stream and pass AudioManager.OnAudioFocusChangeListener
// implementation reference
int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if(result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{
// Play
}
// Implements AudioManager.OnAudioFocusChangeListener
#Override
public void onAudioFocusChange(int focusChange)
{
if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
{
// Pause
}
else if(focusChange == AudioManager.AUDIOFOCUS_GAIN)
{
// Resume
}
else if(focusChange == AudioManager.AUDIOFOCUS_LOSS)
{
// Stop or pause depending on your need
}
}
Related
I'm building a media player and implementing onAudioFocusChange() in a way similar to the docs:
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT
// Pause playback
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Resume playback
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
am.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};
The only weird issue: when leaving my phone sitting with the app in the background and the mediaplayer paused, the service will randomly start playing. When I remove the above code, it doesn't happen. So, onAudioFocusChange() is being called with AUDIO_FOCUS_GAIN as the argument seemingly randomly. Has anyone else dealt with this issue?
onAudioFocusChange() will be called everytime an app request or release the audio focus. This can come from any app, not just yours.
In fact, every notification that plays a sound (eq. Text/mail/...) will gain the focus and then release it. Once another app release the audio focus, your app will gain the focus again thus your resume playback will be called.
To avoid playing when you dont want to, you can keep a boolean that indicates if your app should play:
boolean wantsMusic = true;
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AUDIOFOCUS_LOSS_TRANSIENT
// Pause playback
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN && wantsMusic) {
// Resume playback
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
am.unregisterMediaButtonEventReceiver(RemoteControlReceiver);
am.abandonAudioFocus(afChangeListener);
// Stop playback
}
}
};
How to pause the audio player when I launch the radio app that I develop?
And "vice-versa" , How to pause my app when the user launch the audioplayer when my app is running in background.
You App needs to request the Audio-Focus, more about that here. Once you no longer need it you can abandon the focus, which returns it to the Application which had it previously to yours.
Use OnAudioFocusChangeListener to detect if there are other apps requesting audio focus; if so, pause your audio player.
am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){
// Pause playback
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Resume playback
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
abandonAudioFocus();
}
}
};
Similar SO question is here
I am playing an audio using MediaPlayer class. I have a requirement where audio played by my app should automatically stop when some other songs started playing on the device by the user. How to get notified that some other audio has started playing on the device in my app? Please advise.
UPDATED:
I tried this code, it works for the first time. I.e, when I launch my app and play an audio in my app, my app is playing an audio. Then, When I open some music player and play some other audio there, My app detects notification in "AUDIOFOCUS_LOSS". I am stopping my audio here. Then when again play audio in my app, and then coming back to music player and play some other audio there, this time my app doesn't get detected for this notification, so I couldn't do stop again my audio. This is my issue now.
Could someone help me to solve this please?
public class MyActivity extends ActionBarActivity implements OnCompletionListener, OnPreparedListener, OnErrorListener, OnBufferingUpdateListener, AudioManager.OnAudioFocusChangeListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hclsquad_fm);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
#Override
public void onAudioFocusChange(int focusChange) {
switch (focusChange) {
case AudioManager.AUDIOFOCUS_GAIN:
Toast.makeText(MyActivity.this, "Focus GAINED", Toast.LENGTH_LONG).show();
break;
case AudioManager.AUDIOFOCUS_LOSS:
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
Toast.makeText(MyActivity.this, "Focus LOSS", Toast.LENGTH_LONG).show();
if(mediaPlayer != null)
killMediaPlayer();
break;
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
Toast.makeText(MyActivity.this, "Focus LOSS TRANSIENT", Toast.LENGTH_LONG).show();
break;
default:
}
}
Try this code below. I found this answer here
OnAudioFocusChangeListener listener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK){
// Lower the volume
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
// Raise it back to normal
}
}
};
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int request = am.requestAudioFocus(listener,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
Responsible apps will always get audio focus before playing audio. I don't think it is possible for you to detect when another app plays audio if it does not request audio focus.
Instead of requesting audio focus in your onCreate, do it before you start to play audio:
private void play(){
requestAudioFocus();
}
You are loosing audio focus. You should call
requestAudioFocus();
to get control back.
In my app, I have a media player which play a stream music:
globalVariable.setPlaying(true);
final ProgressDialog mProgressDialog;
mProgressDialog = ProgressDialog.show(MainActivity.this, "Chargement en cours", "La musique est en train de charger", true);
String url = "http://178.32.181.86:18000";
globalVariable.mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
globalVariable.mp.setDataSource(url);
} catch (IOException e) {
e.printStackTrace();
}
globalVariable.mp.prepareAsync();
globalVariable.mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
bouton.setImageResource(R.drawable.pause);
mProgressDialog.hide();
}
});
It works very fine. When I open another app in front of mine, player continues, and it's great. But if I launch another player (like GooglePlayMusic or youtube), my app doesn't stop music and I have two sounds in the same time.
So I want that my music stop if another app need to play music.
Thanks a lot
You should use AudioManager service to receive notification whether you receive/lost audio focus (Managing audio focus). I've done similar thing in a project where when my app starts playing, Google Play pause and vice versa. Use the following code where you are controlling your media playback like (activity or service)-
// Add this code in a method
AudioManager am = null;
// Request focus for music stream and pass AudioManager.OnAudioFocusChangeListener
// implementation reference
int result = am.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if(result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED)
{
// Play
}
// Implements AudioManager.OnAudioFocusChangeListener
#Override
public void onAudioFocusChange(int focusChange)
{
if(focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT)
{
// Pause
}
else if(focusChange == AudioManager.AUDIOFOCUS_GAIN)
{
// Resume
}
else if(focusChange == AudioManager.AUDIOFOCUS_LOSS)
{
// Stop or pause depending on your need
}
}
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// do something - or don't
}
i wrote a podcast audio player and included the audio focus principle.
This is my method, which is called in the onPrepared() method of the media player:
private boolean requestAudioFocus(){
AudioManager am = (AudioManager) getSystemService(AUDIO_SERVICE);
OnAudioFocusChangeListener afChangeListener = new OnAudioFocusChangeListener() {
public void onAudioFocusChange(int focusChange) {
if (focusChange == AudioManager.AUDIOFOCUS_LOSS_TRANSIENT){
pause();
} else if (focusChange == AudioManager.AUDIOFOCUS_GAIN) {
play();
} else if (focusChange == AudioManager.AUDIOFOCUS_LOSS) {
Log.d("MYAUDIOSERVICE","Audio Focus lost");
finish();
}
}
};
// Request audio focus for playback
int result = am.requestAudioFocus(afChangeListener,
// Use the music stream.
AudioManager.STREAM_MUSIC,
// Request permanent focus.
AudioManager.AUDIOFOCUS_GAIN);
if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
return true;
}
else{
return false;
}
}
The problem is:
Everything works finde when starting the app and playing the first podcast. Audiofocus is gained and the podcast keeps playing.
When i stop playback and start another podcast the audiofocus is gained again, but shortly after AudioManager.AUDIOFOCUS_LOSS is receiver by the onAudioFocusChangeListener.
I guess thats the case because my app has the focus, request it again, gains it but at the same time loses the "old" one.
Am i right, and if if thats correct, can i just give up the audio focus when ending playback on the old podcast ?
Thx in advance