Android MediaPlayer suddens stops playing streams - android

I am using the Android's default MediaPlayer to play audio streams. I am facing a problem wherein the stream stops after sometime and doesn't recover post that. If I stop the MediaPlayer object and then again call a Play on it, the same stream plays fine. This means that the Stream was up but my MediaPlayer somehow stopped playing it and never recovered
Is there some solution to this? Can I detect when does my MediaPlayer stops receving and playing data so that I can reinitiate the player?
Thanks

You can listen for errors using onErrorListener
http://developer.android.com/reference/android/media/MediaPlayer.OnErrorListener.html
In my app, I start a progress dialog that says "Recovering". Meanwhile I reinitialise my media Player and seekTo the time it reached before the error, then I dismiss the dialog.

Related

Resume music from another android app

I have an app that runs the Youtube API and plays a video in a certain view. When the video plays, the music from any background apps are paused. I've tried to resume the music with the following code which is run onBackPressed():
private void resumeMusic() {
if(wasMusicPlaying){
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
ExerciseViewer.this.sendBroadcast(i);
}
}
When I go back, I can't get the music to resume. I know its possible because when using snapchat, the background music automatically resumes after a snap video finishes playing but I can't seem to get this functionality on my app.
I've also tried to use "togglepause" instead of play but that does not work either.
I've not tried this, but I think this can be achieved using Android - MediaPlayer.
When entering to your app at the begining of the code, you can check audio is playing. (this should done before calling to youtube api)
mediaPlayer.isPlaying()
if true, you know that a media was playing when someone enter into your application. Store that in your application logic. maybe call it boolean wasplaying,
boolean wasPlaying = mediaPlayer.isPlaying();
when exiting your application, check whether there's was an any audio playing. if any audio is playing, resume it.
if(wasPlaying){
mediaPlayer.start(); //resumes mediaplayer audio
}
I'm not sure about the code and media player does the exact thing, but you can try to use a logic like that.
You can have a look at https://developer.android.com/reference/android/media/MediaPlayer.html
if mediaplayer doesn't work getting the current playing audio, this would help Track info of currently playing music

MediaPlayer is in playing state but plays nothing?

I am testing my app for extreme cases where the user might have bad internet connection so heres what I did.
I have been bottlenecking my internet so that my testing device receives very less bandwidth to stream a url.
Now here is the case :
The media player buffers well for like 5 times and plays the stream. However after few more buffering state and the player goes into this numb mode where I cant even stop it, the player button remains static.
After thorough examination I found this from the verbose :
V/MediaPlayer[Native]: isPlaying: 1
Which means the mediaplayer is fooling my app by saying that it is still playing even though there is no sound coming from the app. Lulz?
Now comes the funny part "the player will start playing the stream only after like 5-10 mins" (when I stop bottlenecking the internet)
listeners attached to the mediaplayer :
MediaPlayer.OnPreparedListener
MediaPlayer.OnErrorListener
MediaPlayer.OnInfoListener / [Also tried OnBufferListener - Same
results]
Any type of help/Suggestion appreciated
Thanks in advance :)

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);
}

Android media player not producing sound even playing

I have created a media player application using Android MediaPlayer API. The app is working fine unless a phone call is received. I am handling phone call events in my app and doing pause and play of media player.
The problem is that After pause and play, when phone call arrives, the song is playing but with no sound from speakers.
One more thing is This is happening when I am using bluetooth headset only.
If anybody faced this problem, please reply suggestions to my question.
I solved this problem by stopping bluetooth's SCO before playing a music track if it is ON.

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.

Categories

Resources