Mediaplayer error once used multiple times - android

I've created a game which is nearly complete, but I've still got one problem:
Once I press a button I want a sound to play, I did this with this code:
MediaPlayer planeSound = MediaPlayer.create(ObjectCanvas.getDrawContext(), R.raw.plane);
planeSound.start();
It works perfectly, but the only problem is when I press it a lot of times, then the MediaPlayer gives a error which is error (-19,0). I searched but I can't find a solution for this. If the error happens the MediaPlayer won't play any sounds anymore untill the actvity is recreated. The game doesn't get a FC, but just the error (-19,0).
I don't know how to fix it, I hope someone can give me some suggestions or explanation.

Go for sound pool, or reuse one instance of media player, or release each instance once you have finished with it.
When done with the MediaPlayer, you should call release(), to free the
resources. If not released, too many MediaPlayer instances will result
in an exception.
etc
It is also recommended that once a MediaPlayer object is no longer
being used, call release() immediately so that resources used by the
internal player engine associated with the MediaPlayer object can be
released immediately. Resource may include singleton resources such as
hardware acceleration components and failure to call release() may
cause subsequent instances of MediaPlayer objects to fallback to
software implementations or fail altogether. Once the MediaPlayer
object is in the End state, it can no longer be used and there is no
way to bring it back to any other state.
See the documentation: http://developer.android.com/reference/android/media/MediaPlayer.html

Related

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.

Check to see if Android MediaRecorder is Recording

I've written some code for Android 2.2 that plays an audio file using the Android MediaPlayer. Without getting into the details of the code, I noticed that there exists a function called
isPlaying()
that allows you to check if an audio file is currently being played by the MediaPlayer. So, for example, when the following snippet of code runs
Toast.makeText(getApplicationContext(), "Sound playing is: " +
mediaPlayer.isPlaying(), Toast.LENGTH_SHORT).show();
it displays the following message
Sound playing is: true / false
depending on whether there's sound playing or not.
When I wrote some code to record sound from the microphone using the Android MediaRecorder, however, I noticed that there did not look like there exists a function called
isRecording()
that checks to see whether a recording is in progress.
So, I was wondering if the onus is on the programmer then to figure out if a recording is in progress by embedding some logic into their code - or if perhaps there indeed exists a way to do this (check if a recording is in progress) by using another in-built function offered by the Android API.
Doesn't look like there is such a function after all. I think it makes sense to try to embed some logic in the code to do this cleanly.
Important
I found a nice workaround to handle this issue, and you can read the workaround right away, but I strongly suggest that you read the entire explanation first.
The scenario
I was trying to find a way to figure out if the mediarecorder had started recording or not, I assumed that calling the start() method on the recorder after the prepare() method was enough, but it turns out, it isn't.
Before you get offended by what I just said, Let me explain the scenario...
I was building a simple audio recording app from scratch, no libraries, copy paste, all hardwork. So I knew exactly what each part of my code is doing. Or at least I thought I did.
Until I decided to try and break my application by clicking on the buttons to start and stop recording like I was playing a piano. And yes, my stop button wasn't even appearing until the mediarecorder's start() method was called.
So I was greeted with a crash and logcat welcomed me with
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
along with
E/MediaRecorder(15709): stop failed: -1007
So I read online and found out that calling stop() right after start() on MediaRecorder causes this problem.
So the biggest question was, how do I detect if it is now SAFE for me to enable the STOP button on my recorder?
The Workaround (Not at all perfect, but it works)
MediaRecorder.getMaxAmplitude() // The maximum absolute amplitude measured since the last call, or 0 when called for the first time
As you can see, the MediaRecorder.getMaxAmplitude() method or the MediaRecorder.maxAmplitude property return 0 when called for the first time and the amplitude after that.
So, instead of allowing the user to Stop the recording right after calling MediaRecorder.start() I am now waiting until the MediaRecorder.maxAmplitude value is greater than zero, at which point I can be sure that the MediaRecorder is initialized, started, and recording, and is in a state where calling stop() is allowed. You can accomplish this by using a runnable that keeps checking until the amplitude is greater than 0. I am already using a runnable for the timer, so I perform the check in that.
Please Note
When working on an emulator, the value returned by MediaRecorder.maxAmplitude is always 0. So, you should use an Android Device to check if everything works as expected.
Now my buttons stay disabled for less than a second when I first start recording. But if I stop and start too quickly, they remain disabled for a bit longer, and I show a "Please wait while the recording starts" message for the user.
I hope this answer helps someone.
Regards!

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

Safe to reset a MediaPlayer in preparing state?

I'm writing an audio player using MediaPlayer that allows the user to skip the actual tune. A skip request might occur at any time, including between a call to MediaPlayer.prepareAsync and the upcoming onPrepared callback. The doc says:
It is important to note that the Preparing state is a transient state, and the behavior of calling any method with side effect while a MediaPlayer object is in the Preparing state is undefined.
Does that include calls to reset, or even to release? Because if it so, then I would either have to wait for the onPrepared callback to reuse the MediaPlayer or allocate a brand new MediaPlayer if I don't want to wait and release the obsolete one on the onPrepared callback, right?
I also built a stream music player and struggled with the preparing state. The worse part of it was that there were some streams where prepare() hung forever downloading (buffering) data without ever calling onBufferUpdate. Calling release did nothing. So, the way I did it was calling reset() on the stuck MediaPlayer from anotherthread after 15 seconds despite the recommendations in the docs. This caused it to throw an exception and brought it to error state. After catching the exception I called release(). This seems to have solved the problem. I hope this will be useful to someone.
In my opinion i will follow the advice in the docs, i found several issues with player in different devices (in some devices is not stable at all reusing the same player).
I think a good option is to have to players allocated, and switch between them when user skips a tune, then you wait for the original player to arrive to prepared state and then you reset it safely.
I'm facing an issue when MP "hangs" at preparing state too long (stream) and i'm trying to stop it using reset(). This causes MP to hang and thus my whole app freezes. Seems like there is no way to stop MP at preparing state. Im thinking on use prepare() wrapped in thread instead of prepareAsync(). Then i'll be able to kill that thread. As for now i did it in following way:
try {
mp.setDataSource(new String());
} catch (Exception e) {
e.printStackTrace();
android.util.Log.d(TAG,"actionCancel(): mp.setDataSource() exception");
mp.reset();
}
and it works 4me.

Categories

Resources