diiiz_
diiiz_

Reputation: 617

How to stop a Media Player of another application?

I'm writing a mp3-player for Android and I want to stop the music when another app like google play music starts to play and the other way round. How to check if another player is running and to stop a player of another app?

Thanks in advance!

Upvotes: 3

Views: 1497

Answers (2)

Nikolai Samteladze
Nikolai Samteladze

Reputation: 7797

If you target Android 2.2 and above, then you can look into Audio Focus. See the documentation here.

From the documentation:

When your application needs to output audio such as music or a notification, you should always request audio focus. Once it has focus, it can use the sound output freely, but it should always listen for focus changes. If it is notified that it has lost the audio focus, it should immediately either kill the audio or lower it to a quiet level (known as "ducking"—there is a flag that indicates which one is appropriate) and only resume loud playback after it receives focus again.

Use AudioManager to request Audio Focus and implement AudioManager.OnAudioFocusChangeListener to respond to Audio Focus changes.

Upvotes: 3

Merlevede
Merlevede

Reputation: 8180

You need to request audio focus, meaning that you're requesting the system to shut other music playing applications. This is the mechanism by which the system moderated playback of different applications.

Also you need to implmement OnAudioFocusChangeListener to be notified by the system when another application want's to play sounds, so that you can stop your own music.

Upvotes: 1

Related Questions