Stop background music from other apps on Android & Fire TV - android

How can I stop other apps that are playing background music when my app opens? I have an app that plays music and videos, but if a background app was already playing music it keeps playing.
On Fire TV I also have a problem that the background music app such as I heart Radio intercepts the play and forward buttons from the remote, so in my app if I try to skip forward it just switches I heart Radio to a new station and makes it start playing even if IhR had playback stopped.
The Netflix and Hulu apps both have solved it, so I know there has to be some way to do it, but my searching only turns up stuff on how to kill your own background service that is playing music, not any background music service.
I am trying to stick to API 8 for the greatest compatibility, but if I have to go up to a higher API I'd like to go no higher than 14 (ICS).

Per the FireTV documentation you need to manage Audio Focus via the requestAudioFocus call (added, as you need, in API8)

Related

User reported that our app kill spotify background music, how to prevent that to happen?

So we got report that our app stop any music playing in the background like spotify, I also just tried it with youtube music and it also got killed
Our app is just a online f&b shop without any sound (but we use mediaPlayer to play logo animation in the beginning) and I dont think I've ever put some code to kill background music
is there any special setting I need to place in manifest to prevent this happening?
it is quite bewildering to me

Can you pause all apps making an audio output in Android Studio?

I was wandering if it would be possible to pause all audio outputs in Android Studio.
As an example, let's say you were listening to Youtube Music or Spotify, what code wouldbe able to pause them. Not mute them. Pause them. Additionally, if it's possible could you make it that it goes back a few seconds (etc. 5s or 2s). Also, I would like it not to require to use any API's as I'd like it to be universal but if that's not possible, I would like it to be able to pause and revert a few seconds in Youtube Music (Premium) and the default Android Music Player.
Thank you.

How to make background sound quiter

If you have an app like Spotify playing music in a background and try to listen to some sound in Google Chrome at the same moment, the background music becomes quieter at this very moment while Chrome playing the sound and then comes back to normal level.
I am wondering how I can do the same from my own Android app, namely, to make volume of all other applications lower for some time, but allow my application to play sounds with normal volume at the same time. I tried to find the answer, but usually all recommendations are about how to change volume globally.
This is a feature called audio ducking- the sound that gets quieter is said to be ducked. The way Android does it is through audio focus. Your app needs to request focus, and the other app's sound will be changed appropriately. The documentation on it is at https://developer.android.com/guide/topics/media-apps/audio-focus. The mechanism changed in Android 12, so you'll likely have to have different code for older versions and newer ones to make it all work.

How can I auto-pause the audio player in my app when another app starts to play audio?

My app plays an audio file. I want my app to pause the audio playing if another apps starts to play audio. I don't want the two audio sounds mess up each other. My app will resume the audio playing after the other app finishes playing its audio. How can I make it? Thanks.
Depending on how your application is running ie. while app is running in foreground (users is looking at your screen) or if your running a "service" which is currently in the background and you wish music to continue playing (songza, slacker radio etc.) how you handle these things will be different. Android uses internal system states to determine which apps should be out-putting audio to the speaker. All this is handled by the audio manager requestAudioFocus() releaseAudioFocus() will be the system calls you will want to look at the most. For a more detailed explanation see android developer api where you this is explained and documented quite well http://developer.android.com/guide/topics/media/mediaplayer.html

Android: when implementing a music player in my app, can i make sure there is no other media playing playing music right now?

I want my app to play music while it is running, but I don't want the music to overlap with the music currently being played from a different application (android music app or other external music app such as pandora, grooveshark or winamp).
My question is: is there a way to make sure that nothing else is playing right now regardless of the source?
Thanks!
As of android 2.2 you can use AudioManager.requestAudioFocus(), other audio players should listen for this focus change request and can stop/pause/lower volume of their audio according to what type of audio your focus requests. However not all audio playing apps have bothered to implement this yet.
To be nice you should also listen for audio focus change requests from other apps and pause your apps audio accordingly.
On earlier versions of android calling mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); will usually stop any other music from playing

Categories

Resources