Android, MediaPlayer crashes after random amount of times - android

I have this simple bit of code which is executed everytime the NEXT button is being clicked:
mediaPlayer.Stop();
mediaPlayer.Release();
mediaPlayer = MediaPlayer.Create(this, uri);
btn_StartOrPause.SetImageResource(Resource.Drawable.btn_pause);
So the current song is now stopped and a new song with a new uri is being created. This works around 2 times in a row until the create line crashes saying only:
Java.Lang.IllegalStateException:
No content.
Am I using the player wrong? Why is it crashing sometimes after one successful click, sometimes straght away? The uri is always correct.

May you can use
mediaPlayer.stop()
mediaPlayer.reset()
and then use
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepare();
Maybe release() can make the state of MediaPlayer Error.

After trying this out a gazillion times, it seems that THIS is the right syntax for ending one song and then recreating the other:
mediaPlayer.Stop();
mediaPlayer = MediaPlayer.Create(this, uri);
Note that my mediaPlayer is a static variable!
Thanks all!

Related

Mediaplayer is slow, how to overcome this?

Problem:
I've created a single-radio app with MediaPlayer but prepare() method takes too much time (12-15 Seconds) and as a result when user hits the play button he/she waits for so long.
Is there anything I can do about it(I need to make it faster)? I found Exoplayer but little documentation/tutorials were found about it ;/
To be more specific here is what code I've written (Strings[0] is the URL)
mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(strings[0]);
mediaPlayer.prepare();
I am open to any new suggestion or ideas

Android MediaPlayer automatically plays the next song

I have very basic media player code:
AssetFileDescriptor FileDescriptor = getAssets().openFd(musicPth);
MusicPlayer.setDataSource(FileDescriptor.getFileDescriptor());
MusicPlayer.prepare();
MusicPlayer.start();
For some reason when the song finishes, the player automatically plays the next song in the folder!
I've tried setOnCompletionListener and setLooping functions but it seems they don't effect the player and it just goes to the next song.
If I exclude the second mp3 then the player loops the same song which is what I want.
Very strange behavior, am I missing something?
I have seemed to fix my problem by changing the setDataSource() function to this:
AssetFileDescriptor FileDescriptor = getAssets().openFd(musicPth);
MusicPlayer.setDataSource(FileDescriptor.getFileDescriptor(),
FileDescriptor.getStartOffset(),
FileDescriptor.getLength());
FileDescriptor.close();

Can i play multiple songs with single MediaPlayer

I have to 2 songs.I used radio buttons for both songs so when select first it should play me the first song and when i click on second. Second song should be played.I have used Play,pause and stop button so when i select first song and click play first song should be played.how to use media player for 2 songs.Previously i used 2 media players for different songs. How to use one media player.
Previously I have used this statements for two songs
mediaPlayer = MediaPlayer.create(getApplicationContext(),R.drawable.inno);
mediaPlayer1 = MediaPlayer.create(getApplicationContext(),R.drawable.rocky);
My question is i want to use only one media player for both songs
You better stop being lazy and search for the solution on your own aswell. #1Up pretty much answered your question. For your second question: This is Uri
Keep using two mediaplayers, in cases like this there is no problem doing that. Using only one reference would mean you'd have to recreate it each time you want to change clip, or stopping it and calling setDataSource(context, URI).
If you use only one reference to mediaplayer the user will have to wait for the clip to be ready each time it has to be played, while in your implementation both sound clips are ready to be played at any time.
Anyways, here is a setDataSource example:
MediaPlayer mp = MediaPlayer.create(context, firstSongUriOrRes);
public void play(int clip)
{
if(mp.isPlaying()) //Stop the mediaplayer if it's already playing
mp.stop();
switch(clip) //Choose the clip to be played
{
case 0:
mp.setDataSource(context, firstSongUriOrRes);
break;
case 1:
mp.setDataSource(context, secondSongUriOrRes);
break;
}
mp.prepare();
mp.start(); //Start the mediaplayer
}
Another way to use setDataSource is to place the audio files inside the asset directory and use this code:
AssetFileDescriptor fd = context.getAssets().openFd("pathInsideAssets/fileName");
mp.setDataSource(fd.getFileDescriptor(), fd.getStartOffset(), fd.getDeclaredLength());

sounds not playing with mediaplayer after playing some sounds

I'm developing an android application which is collection of 100 sound effects.
After I play for instance 25 of the sounds, I can't play anymore and I have to close the application and wait for some minutes then open the application and now I can play other sounds.
final MediaPlayer mp81 = MediaPlayer.create(MainActivity.this, R.raw.a81);
I play the sound using the below code:
mp81.start();
I stop the playing sound using the below code:
mp81.seekTo(0);
I also used stop() method but the problem were still existing.
is there any other method i have to add?
Please note: consider using SoundPool for playing short sounds.
Regarding your use-case: you initialize your MediaPlayer instance using the static create() method which means you create a new MediaPlayer object for each sound instead of reusing an existing instance and just changing the data source. This might negatively affect the performance of your app. I suggest that you create an array of paths to your sounds and then instantiate the MediaPlayer like this:
MediaPlayer mp = new MediaPlayer();
try {
mp.setDataSource(yourArray[x]);
mp.prepare();
mp.start();
} catch(Exception e){
e.printStackTrace();
}
Consult the MediaPlayer Document for more information.

One Android MediaPlayer Object plays several sounds at once

I have ONE object MediaPlayer mediaplayer. I use it to play different sounds, one after another.
mediaplayer = MediaPlayer.create(context, ResIdMusicONE);
mediaplayer.start();
// some user input
mediaplayer.release();
mediaplayer = null;
// some other user input
mediaplayer = MediaPlayer.create(context, ResIdMusicTWO);
mediaplayer.start();
// some user input
mediaplayer.release();
mediaplayer = null;
Sometimes is works fine. But sometimes the two sounds are played at the same time. And at positions, where mediaplayer should already have been released and be equal null.
Thanks for the help.
If I were you I would use the SoundPool class for this. With SoundPool you can set the number of streams to play at the same time, so by setting that to 1 you can just call play() over and over and the most recent call to play() will be the only sound that you hear.
Take a look at my post a while back. It has an example of the SoundPool class in the question.
Edit:
Have you tried creating a new instance and calling the prepare() every time you want to start a new sound?
mediaplayer = new MediaPlayer();
mediaplayer.setDataSource(path);
mediaplayer.prepare();
mediaplayer.start();
Though actually now that I think about it, I'm pretty sure you only need to do that if you are using a file from the sdcard not from your resources... Hmmm.

Categories

Resources