Domanda

I am making an app in which I've made a Service which plays Music from URLs. The thing is that my music Service is playing music correctly BUT when user plays any song with Native music player then BOTH(Native Player and My Music Service) are playing music simultaneously. I want to stop My Music Service when user started playing music with native player.

  • Is there any Broadcast Intent which i can register to Detect the music player is started?

  • Is it possible to detect Music player Started?

  • Any Other Solution?

Any suggestions would appreciated.

È stato utile?

Soluzione

I'll suggest a different approach, that I believe it's the correct approach.

the issue on your approach is that you're suggesting to check for one specific app. And there're tons of different music players, plus radio players, plus video players, plus games... and all of those should stop your music in case they want to play something.

So how you do it?

It's all explained in the Android Developers website. You have to register an OnAudioFocusChangeListener, so, whenever a different app request to have the audio focus, your app can stop the music.

Altri suggerimenti

Step 1: Detect if the user has opened native music app. For this , you need to know the package name of your native music app.

Then refer to my answer here: Android how to know an app has been started and range apps priority according the starting times

Using that , the list taskinfo will have the list of all running activities, and as explained there, the first element of the list will be the activity in the foreground.

STEP 2: Once you detect native music app being activated using STEP 1 (by polling for it in the background) , then stop your app's service.

NOTE: You should do this in a background (using asynctask) or another service.

NOTE 2: The limitation of this method is that you can't actually stop the music player when the user clicks play in the native music app, since this method will help you detect only if the native music app is opened or not.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top