MediaPlayer pause() behaviour - android

I am using a MediaPlayer to stream audio from a URL.
According to the documentation, calling the MediaPlayer pause followed by a play will resume from the point where it was paused.
I am wondering how this works with a live audio stream. When I call pause is the MediaPlayer creating some sort of buffer of all the incoming data, and storing it until I call play again?
If this is indeed the case, is there a max size on this buffer? I am mainly concerned about a user pausing the MediaPlayer and it using a lot of memory while it stores incoming audio data.

As, I understand you are using Mediaplayer for streaming audio from a URL.. something like radio channels. In this process, you are using buffers. So, the behaviour you are getting is quite obvious. When you pause, your data will continue to store and on resuming the stream, it will start from the point it was paused.
But streaming should not behave in this manner unlike stored audio which start from the point it was paused. Streaming audio should always start from the live streaming at that point. So, onPause, you should free the buffers. When the user resumes again, you can restart the stream the way you did it first time. This is how the behavior should be.
If you check out, normal radio streaming is implemented in most of the radio streaming apps.

Related

How to know if, Android MediaPlayer buffering has stopped deliberately(not because of network issue) because enough buffering has done already

Using MediaPlayer.OnInfoListener I am able to see when MediaPlayer is temporarily pausing playback internally in order to buffer more data.
But as MediaPlayer obeys progressive HTTP download, Ideally it pauses buffering since enough buffering is already done, If user continues to
watch video it will buffer next chunk of data.
I want to know exactly when buffering has stopped because enough buffering is done. (not because network issues, congestion, low bandwidth etc.)
Am I thinking it in wrong way?
If not, Is there any way to find this?

Stopping media players in android

I have an application in android which uses many mediaplayers from a service.
Seeking a way that pressing a button, stop the audio.
The problem is that I can not know the name of the media player is playing when I press the button.
Is there any way to stop all mediaplayers? or to detect the name of the mediaplayer is playing and stop it?
I appreciate any help
Thanks and regards
At most, you need two MediaPlayer objects. While one is playing, call prepare or prepareAsync on the other. After the 2nd finishes preparation (in OnPreparedListener if using async), call setNextMediaPlayer on the MediaPlayer object that is currently playing. If the current one has finished before preparation completed just go ahead and call start instead.
Be sure to call release in the OnCompletionListener for each player, and if there are more files to play simply call setDataSource and prepare (or prepareAsync) for the next media. You can continue this pattern indefinitely without using extraneous resources, and the playback should switch as seamlessly as Android is able to make it.

Why do we use prepare() in a media player?

I am unable to clear myself that why do we need to use prepare() method in Mediaplayer. Why start() independently doesn't work in music players...
The prepare method collects metadata about the file or stream to be played, which may be necessary for proper function of the player itself and related components (like UI). The fact that you can call prepare and prepareAsync separately from calling setDataSource or start is simply a means of allowing the developer control over when and how things happen to suit his/her particular circumstance. Particularly for streaming media, preparation may take a significant amount of time, and so doing things the same way all the time will not be ideal in every situation.
suppose if you want doing some work that can be possible when media player is collecting infoemation then what you do. if start() work for both what happened if media player is collecting information about media. this will be treated as playing and it crashes completely. these are the states and has there works.

Android MediaPlayer streaming stops on network change

I am streaming audio using MediaPlayer on Android.
When the device moves from Wi-Fi to the cell network or vice-versa, the MediaPlayer stops playback.
Typically there are a few seconds-worth of audio in the buffer, so playback does not cease immediately.
Ideally I would like to pick up the stream for uninterrupted playback, but I cannot see how to do it.
I am working with both mp3 files hosted on the server and a live broadcast stream.
From a servers point of view, changing network mode from WiFi to 3G (visa versa), will look like a brand new connection from a separate IP's (client).
If the server that you are downloading from does not support tracking the stream (e.g number of seconds, sequence, byte) (unlike media-servers),It will have to start serving your mp3 from 0 byte again.
If your URL is pointing to a MP3 file located at a standard HTTP server, your situation will be what to expect. You should look into using a Media streaming server, so you could resume downloading/streaming at your choice. When you receive the intent that the connection is lost/resume, you could point your mediaplayer to the new URL with file-position in the URL (e.g seconds=19, bytes=57365).
Not sure if this helps you, but it explains a bit whats going on "behind the scenes".
Try setting your setOnCompletionListener and setOnErrorListener. In on complete with a live stream you can just call prepareAsync() again and this will kick off the stream again. There is no graceful way of doing this really unless you write your own media framework.
You can also listen in you onError() for the MEDIA_ERROR_SERVER_DIED you can then fire off the prepareAsync() again.
You'll find that the MediaPlayer will either Error or Complete. If you handle both these callbacks the very least you can do is restart the stream on change of network, as for smooth playback.. that would require custom mediaframework as the android one is pretty shoddy.
I don't know why your media player is stopping, but maybe you could add an onReceive method and put "mp.start()" in the method to make it restart playback.
Android, How to handle change in network (from GPRS to Wi-fi and vice-versa) while polling for data
You might need to make a separate class, but that should explain how to create a method that is called when you switch networks, at which point you could call "mp.start()" to resume playback (assuming mp is your MediaPlayer).
This assumes, of course, that your MediaPlayer is only being paused when you are switching networks, not stopped.
As Vidar says, reestablishing the connection will be treated by the server as a new connection.
It appears that I have to double-buffer the audio playback, which means building a custom media player. This can provide continuous audio, but it will still skip when listening to a live stream.
The MP3 file is a bit easier because I can know the playback position. Not so with the live stream.
As gmaster says, I'll need a broadcast receiver to establish a new connection when the network changes.
The audio buffer from the previous network connection should continue to playback while a new audio buffer is filled via the new connection.
When the new buffer is full enough to start playback I can switch playback to it.
If I am streaming a file, with server support and a little bit of work I can ensure that the current playback position data is in both buffers and switch seamlessly.
As the live stream buffers cannot be synchronized, there will inevitably be a glitch when they switch.
A larger buffer will avoid audio drop-out if the connection takes a while to establish, but will delay the first start of playback. An MP3 file can be downloaded and fill the buffer faster than real time, but the live stream will buffer in real time.
Chris.Jenkins mentions some MediaPlayer methods that can help but points out that this does seem to need a custom framework. It will need to handle the conditions he mentions and others.
If I can make it look pretty I'll post it here. I'm going to keep the question open.

AsyncTasks and Audio

I am trying to play multiple audio files, one after the other and am currently using AsyncTasks to prepare and start the mediaPlayer but have failed to find a good way to move on the to next track at the end of the current one. Not every audio file will be played every time, and it's playing is decided by a boolean value.
Any help is much apprecieated.
I guess you have read android-sdk/docs/reference/android/media/MediaPlayer.html , it says:
When the playback reaches the end of stream, the playback completes.
If the looping mode was being set to truewith setLooping(boolean), the
MediaPlayer object shall remain in the Started state. If the looping
mode was set to false , the player engine calls a user supplied
callback method, OnCompletion.onCompletion(), if a
OnCompletionListener is registered beforehand via
setOnCompletionListener(OnCompletionListener). The invoke of the
callback signals that the object is now in the PlaybackCompleted
state. While in the PlaybackCompleted state, calling start() can
restart the playback from the beginning of the audio/video source.
So you may set a new source, prepareAsync then start in completion callback. In this way , you get continuous playback, but it is not seamless.
Doubtful using MediaPlayer for this will work like you want it to. Try this tutorial:
http://www.droidnova.com/creating-sound-effects-in-android-part-1,570.html
If that doesn't work you'll probably have to mix the sounds together yourself them stream that result directly to the hardware using AudioTrack. That's more low level, but it will give you the most control. It just depends on what you are doing if the AudioManager solution will work for you or not. It's definitely the simpler route. But, if you're trying to line up two samples so that when one finishes the next begins, like in a music app, you probably will have to mix and stream that audio yourself.
http://developer.android.com/reference/android/media/AudioTrack.html
Algorithm to mix sound

Categories

Resources