Search code examples
androidandroid-intentandroid-serviceandroid-music-player

how to detect when Music is played using Native Music Player?


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.


Solution

  • 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.