android MediaPlayer - illegalstateException on MediaPlayer - android

I have the following piece of code.
ex:
if(player1 != null){
if(player1.isPlaying()){ //check if it playing
//other code
}
}
QUESTION 1:The condition check for null always passes even though the media player as finished playing and i release the player on oncompletion.
//release on completion of the player
player1.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
Log.d(TAG, "Media player has completed playing");
}
});
QUESTION 2:
IF the player HAS finished playing, a check for null still returns false and it fails as isPlaying giving an illegalstateexception.

Releasing the player doesnt nullify the instance. Add null after release if thats what you want.
player1.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
mp = null;
Log.d(TAG, "Media player has completed playing");
}
});

Related

Android Media player onClick issue

I'm using MediaPlayer for playing sounds onClick. Until the sound is finished the click event is not play the sound again. How can it play the sound again on click, when the sound is currently playing?
final MediaPlayer mistake = MediaPlayer.create(getActivity(), R.raw.mistake);
tv_mistake.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mistake.start();
}
});
You must create a new MediaPlayer object to play the sound again like so.
tv_mistake.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (mistake != null) {
mistake.release();
mistake = MediaPlayer.create(getActivity(), R.raw.mistake);
}
mistake.start();
}
});
You can read more about MediaPlayer in the following links.
Also a quite similar question
MediaPlayer, MediaPlayer Tutorial, MediaPlayer Tutorial From Google
Most of the errors in MediaPlayer comes due to improper handling of different states of its object.
You should release MediaPlayer object after completing playback or before calling start() again.
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.
Create a MediaPlayer object as:
Mediaplayer mediaPlayer = null;
And call playMistakeSound() on button click:
tv_mistake.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
playMistakeSound()
}
});
Implement playMistakeSound() as:
void playMistakeSound() {
try {
// releases MediaPlayer object before calling create() again while previous is still playing
if (mediaPlayer != null){
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = MediaPlayer.create(getActivity() /*Context*/, R.raw.mistake);
// this will release MediaPlayer as soon as it completes
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if (mp != null) {
mp.reset();
mp.release();
mediaPlayer = null;
}
}
});
mediaPlayer.start();
} catch (Exception e) {
// log exception and handle
e.printStackTrace();
}
}

Stopping media player before playing next audio

I am having a listview in my app. Each listview item has button which will play an audio from a url received from web service. But my problem is that if I click play button from the next item then both start playing together. I am having problem in this. I want only one to play at a time. Right now I am creating new Media player object everytime button is clicked, but I also tried creating a single global object but in this case it only plays first time and not after it. What is the possible solution of it.
finalHolder.iv_sound.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try {
MediaPlayer mp = new MediaPlayer();
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
mp.prepare();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
//startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(birdsUrlList.get(position).getUrl_video())));
} catch (Exception e) {
e.printStackTrace();
}
}
});
Make the mp variable global and remove this:
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
because if you take a look at this reference http://developer.android.com/reference/android/media/MediaPlayer.html#StateDiagram it says:
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.
And when you call mp.release(); the media player WILL go to that state.
Then make your onClickListener look something like this:
#Override
public void onClick(View v) {
try {
if (mp.isPlaying()) {
mp.stop();
mp.reset();
}
mp.setDataSource(birdsUrlList.get(position).getUrl_audio());
//... and so on
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
mp.reset();
}
});
The trick there is to make it back to Idle state so you can set the new data source and start playing again. It's all about the states...

How to play audio user define times in android?

Can anyone give me a link of function to play audio file using user define time.. I am using handler but something is missing..
I am also done infinite time audio play but we need user defined times audio play.. Thanks...
Infinite time play audio is done using below function..
#Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
// mMediaPlayer.setVolume(0.2f, 0.2f);
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
mMediaPlayer.start();
}
});
mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
I just wanted how to play audio x times in android using Thread??
I am updating my function but i was not got 100% on this..so plz help
my updated function is as bellow
public void Repeat_mantra()
{
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
// mMediaPlayer.setVolume(0.2f, 0.2f);
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer arg0) {
mMediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.shri_krushna_fast);
for(count=1;count<5;count++)
{
mMediaPlayer.start();
while(mMediaPlayer.isPlaying());
}
}
});
// mMediaPlayer.setLooping(true);
mMediaPlayer.start();
}
But this function is running then other clicks/or user interface are not appear because of "while(mMediaPlayer.isPlaying());" so any help me for how to update my function with Thread??

Play Sound File from res/raw using MediaPlayer

I want to play Sound Files from raw folder using MediaPlayer but I don't want to use MediaPlayer.create() method, since I want to play it multiple times and want to retain MediaPlayer object. I was successful in compiling the following code but it plays nothing and I can't hear any sound.
public void STARTSOUND(SOUND sound) throws IOException {
switch(sound) {
case SOUND_MENUBG:
Uri uri = Uri.parse("R.raw.mainmenu");
PLAYSOUND(uri);
break;
}
}
public void PLAYSOUND(Uri file) throws IOException {
mPlayerLoopSound.setDataSource(GameManager.getInstance().getCurrentActivity(),file);
mPlayerLoopSound.prepareAsync();
mPlayerLoopSound.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
if (!mPlayerLoopSound.isPlaying()){
mPlayerLoopSound.setVolume(1.0f, 1.0f);
//start the player
mPlayerLoopSound.start();
}
}
});
}
The onPrepared() method never gets called..
Am I doing anything wrong?
private void playButtonSound(final Context context, final int resourceId)
{
final MediaPlayer mediaPlayer = MediaPlayer.create(context, resourceId);
if (mediaPlayer != null)
{
mediaPlayer.setOnCompletionListener(new OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mediaPlayer)
{
if (mediaPlayer != null)
{
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
}
});
mediaPlayer.start();
}
}
Call this method like this.
playButtonSound(PopScreenActivity.this, R.raw.soundfilename);
First, hopefully you're initializing your MediaPlayer with:
mPlayerLoopSound = new MediaPlayer();
Next, make sure you call:
mPlayerLoopSound.setAudioStreamType(AudioManager.STREAM_MUSIC);
which:
Sets the audio stream type for this MediaPlayer. See AudioManager for a list of stream types. Must call this method before prepare() or prepareAsync() in order for the target stream type to become effective thereafter.
Then, in your OnPreparedListener(), when onPrepared() is called, notice the parameter it has: MediaPlayer mp. This is the MediaPlayer that is ready for playback, so you can use it:
#Override
public void onPrepared(MediaPlayer mp) {
if(!mp.isPlaying()) {
mp.setVolume(1.0f, 1.0f);
mp.start();
}
}

Releasing consumed memory after using MediaPlayer

I'm developing an application containing 100s of sound effects.
after playing some of the sounds the application force closes. I understood that the problem would be consuming all the available space of the memory. I tried using Release() method but after this method is called I'm not able to play the sound again.
I also tried using onDestroy() method to set the object of the mediaplayer to null but since these kind of objects must be final I can't do this.
What is your suggestion?
Here is my code:
final MediaPlayer mp1 = MediaPlayer.create(MainActivity.this, R.raw.a1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (i[0] == false)
{
mp1.start();
Toast.makeText(getBaseContext(), "horn", Toast.LENGTH_SHORT).show();
b1.setBackgroundResource(R.drawable.stop_button);
i[0] = true;
}
else
{
mp1.pause();
mp1.seekTo(0);
b1.setBackgroundResource(R.drawable.horn);
i[0] = false;
}
}
});
mp1.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
b1.setBackgroundResource(R.drawable.horn);
}
});
Have you tried something like this?
You can call release() in OnDestroy() method.
#Override
public void onDestroy(){
super.onDestroy();
mp.release();
}

Categories

Resources