Android media player(android.media.MediaPlayer) two music concurrently - android

Before open my application I played some of the music from default music player. With that background music I opened my application with the mediaplayer (android.media.MediaPlayer) to play the mp3 available in assets.
The default doesn't stop the music and I am getting two music concurrently from the device.
How do I stop or pause music of the default music player whenever my app started playing the music?

I think thats impossible. The default music player is a different app and out of your control. android does not allow controlling other app features from your app.

I had the same problem. I was trying to stop the background music of the default music player (if it is playing) when my VoIP SIP application receives an incoming call. I came up with:
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setStreamMute(AudioManager.STREAM_MUSIC, true);
This does not stop or pause the music, but it make the music stream silent. Do not forget to unmute it later. This solution works well only if your second sound does not use the same stream.

Related

Play and Pause external audio from my app?

I am currently making an application that uses the proximity sensor to play / pause and skip music. If a hand is near for under a second, it plays and pauses, if it's over a second it will skip the song. I have the "long" and "short" readouts measured properly, but I do not know how to play or pause music playing in another app from my app. (Ex: Say a person is listening to YouTube Music, I need my app to be able to toggle the play state).
What lines of code would let me play or pause or skip songs?
I am not sure about all apps. But this code works for some music app.
//Pause music
AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
am.requestAudioFocus(null,AudioManager.STREAM_MUSIC,AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
//Play music
am.abandonAudioFocus(null);

How to check if external media player is playing in android?

I want to stop my media player if external media player is playing.
I've implemented following code but it is called even when my app media player is playing.
if (audioManager.isMusicActive()) {
return;
}
How to distinguish between in-app media player and external media player?
Any help would be appreciated.
Two or more Android apps can play audio to the same output stream
simultaneously. The system mixes everything together. While this is
technically impressive, it can be very aggravating to a user. To avoid
every music app playing at the same time, Android introduces the idea
of audio focus. Only one app can hold audio focus at a time.
Further,
A well-behaved audio app should manage audio focus according to these
general guidelines:
Call requestAudioFocus() immediately before starting to play and
verify that the call returns AUDIOFOCUS_REQUEST_GRANTED. If you design
your app as we describe in this guide, the call to requestAudioFocus()
should be made in the onPlay() callback of your media session. When
another app gains audio focus, stop or pause playing, or duck the
volume down. When playback stops, abandon audio focus. Audio focus is
handled differently depending on the the version of Android that is
running:
You can check the more detail from developer link.

Android: Use running MediaPlayer

I' developing an Android app and in this app I've got a Android MediaPlayer and I want to ask, if I can access with this MediaPlayer to a running MediaPlayer, that was started by an other app?
For example I start a song with the Android Music app and the user opens my app. Is there a method to access and control this stream of the Android Music? So I can stop the music with stop() and start it start() and all the other functions?
If you are asking if there's a way to control a Media Player running in some other app from your app, then no. There is no way to do that.
If the other app is a service though, and that service allows control over its media player through intents, then you could control the other media player, but I'd be surprised if you got that lucky.

Mixing audio with music player and AudioManager.requestAudioFocus

Is it possible to mix my app's audio with the music player audio?
I find that by default, android does mix the audio of the music player with my app.
However this stops working after calling AudioManager.requestAudioFocus method. Meaning, if the music player is playing, when my app starts playing, it stops the music player. I want them to play at the same time.
I request the audio focus so I can know when other interruptions such as phone calls have happened. I do want to stop my app's audio when a phone call happens.
My audio plays sin waves, it is a medical device. I request the audio focus with AudioManager.STREAM_MUSIC and AudioManager.AUDIOFOCUS_GAIN.
I am using AudioTrack to play them.

how to stop the native android service playing music

I am developing a media app.
If a user plays a song with the native android application for playing a song and then opens my application then i want to stop playing that song in background before user opens my application. At present both songs are being played at a time.
You do not "stop playing that song in background before user opens my application". You request the audio focus using AudioManager.
You can have your program run the isPlaying() routine check if something is playing and take action against the media player if needed.

Categories

Resources