0

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.

QAMAR
  • 2,684
  • 22
  • 28
  • I have a solution if you are OK with your app stopping its music if user *opens* native music app ( but not necessarily starts playing music thru native app). Please let me know if that scenario is OK for you. If it is, I will post answer. – SoulRayder Jan 23 '14 at 10:53
  • @Gautham please suggest solution! – QAMAR Jan 23 '14 at 10:56

2 Answers2

2

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.

Budius
  • 39,391
  • 16
  • 102
  • 144
0

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.

Community
  • 1
  • 1
SoulRayder
  • 5,072
  • 6
  • 47
  • 93
  • How about music played from Widget? What if user played music using any third party music player? – QAMAR Jan 23 '14 at 11:09
  • It will work for third party music player, ( again you need to get package name), I am not sure about widget though. – SoulRayder Jan 23 '14 at 11:11
  • I think it will not work in case of widget:), however your suggestion is good, I'll accept your ans if no other best solution is found. – QAMAR Jan 23 '14 at 11:16
  • Sure. I will try and find a solution for that too if possible. :) – SoulRayder Jan 23 '14 at 11:16
  • I think you can detect music widget. If you select play, it should be launching the native music app's task. So also find out which *task* is in the foreground when you play music widget, and poll for that task. – SoulRayder Jan 23 '14 at 11:47
  • It should be present in a member of **taskinfo** itself. Just check – SoulRayder Jan 23 '14 at 11:49