Android audio playing - android

I am playing audio from http link using media player and I release the mediaPlayer onStop() of the activity in which the mediaPlayer plays the song. I want the song to continue playing unless user exits from the app ie exits from the home page. how can i stop and release the media player from the home page?

You're going to need a Service that plays the music and then your App will just send message to the service.

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 stop music in my service when youtube app resumes video?

In an android application, I have written a service that plays music at the background. I have implemented OnAudioFocusChangeListener that listens to change in audio focus. The problem is I dont recieve a call back to onAudioFocusChange() in the following situation.
Play music in my app and put the app in background. (Music keeps playing at the background).
Start youtube app, and play any video. This gives a callback to onAudioFocusChange() where I pause my media player.
Now I press home button for youtube(youtube stops playing).
Go back to my app and play music again and put the app in background(music keeps playing at background).
Now start youtube and resume the video(stopped earlier), This does not give me a call back to onAudioFocusChange().
Note : Only resuming a video does not give a call back to onAudioFocusChange(), but playing a new video, gives a call back to onAudioFocusChange().
Found a solution for my own problem. This is how I solved the issue.
Every time your application loses audio focus, your have to request for audio focus and then play music. This will allow you to listen to the changes in audio focus through onAudioFocusChange().
public boolean requestFocus()
{
return AudioManager.AUDIOFOCUS_REQUEST_GRANTED == mAudioManager
.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}

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.

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

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.

Categories

Resources