Android Media Player Release won't stop playing - android

Here's my scenario:
I have a timer that counts down from 20 seconds. At 13 seconds, a sound starts playing. At 0 seconds, I stop the current sound, load up a new sound and play that new sound. Once that sounds finishes, I load up the previous sound, set it to loop, and start it.
This is the logic for hitting 0 seconds:
mp.stop();
mp = MediaPlayer.create(mActivity, R.raw.second_sound);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp = MediaPlayer.create(mActivity, R.raw.first_sound);
mp.setLooping(true);
mp.start();
}
});
Once the last sound starts playing, calling mp.release() doesn't stop the player. Any ideas as to how to stop the player?
Note: mp.release() works during the first time I start playing and during the second sound but not during the looping sound.
Solution: My global MediaPlayer is also named "mp". Oops.

You're getting the mp MediaPlayer objects mixed up a little. If mp.release() is being called outside of the onCompletion() method, it is not referring to the looping instance created there. Change the identifiers in onCompletion() to refer to the "outer" mp. For example, if your code is in MainActivity:
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener()
{
#Override
public void onCompletion(MediaPlayer mp)
{
MainActivity.this.mp = MediaPlayer.create(MainActivity.this, R.raw.chirp);
MainActivity.this.mp.setLooping(true);
MainActivity.this.mp.start();
}
}
);

Why are you creating new media player every time and not releasing old one onCompltion? did you try setDataSource?

Related

MediaPlayer creates new MediaPlayer instead of stopping

I have created a list adapter which is for a list of sounds. The user presses one and it plays the sound on a loop until they press to stop it. It shouldn't let more than one sound play at a time.
MediaPlayer mp;
public void playSound(int position){
if(position!=-1){
mp = MediaPlayer.create(mContext, ints.get(position));
if(!mp.isPlaying()) {
mp.setLooping(true);
mp.start();
}else{
mp.stop();
mp.release();
}
This does the exact opposite of what I would like. It doesn't stop playing the sound and instead plays multiple instances of sounds at the same time.

Android MediaPlayer.start does not start

I have yet to find an answer to this.
I have a local file (R.raw.Bob); and I am trying to use MediaPlayer to play the file.
Sometimes it plays, sometimes it does not. I have another file which plays seemingly fine every time.
My activity flow is like this: In onCreate I do the following:
MediaPlayer mBackground = MediaPlayer.create(MainAct.this, R.raw.background);
mBackground.start(); // Works as expected.
Now in a different part of the activity I have the following:
MediaPlayer mBob= MediaPlayer.create(MainActivity.this, R.raw.Bob);
mBob.start();
And nothing occurs. I have used Log.i() and the execution goes through the relevant code but the file does not start.
Why does MediaPlayer sometimes work and sometimes does not, and is there a more reliable way of playing sound files?
Try this to start:
MediaPlayer mBob = MediaPlayer.create(MainActivity.this, R.raw.Bob);
mBob.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(final MediaPlayer mp) {
mp.start();
}
});
and this to stop:
mBob.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.stop();
}
});

Playing sounds in Android in certain order

In an Android app I am using MediaPlayer to play sound files.
This is just for personal use and will not be published.
I have several references to the sound files:
MediaPlayer dooropen = MediaPlayer.create(MainActivity.this, R.raw.dooropen);
MediaPlayer doorclose = MediaPlayer.create(MainActivity.this, R.raw.doorclose);
//...
For example the length of the dooropen sound clip is 2 seconds so after I play it I sleep for 2.5 seconds and then play the doorclose sound clip, like so
dooropen.start();
try{ Thread.sleep(2500); }catch(InterruptedException e){ }
doorclose.start();
The issue I am having is some of my sound files are not playing in the order I have them in.
There does not seem to be any reason why certain sound files do not play, because if I play them at the top of my onCreate() procedure they all play, it is only when I try and play them in a certain order.
You should implement the setOnCompletionListener() of the mediaplayer to get a callback when playback has completed and then load another audio file that needs to start playing.
See MediaPlayer Documentation about the mediaplayer state.
Yes you can use MediaPlayer along with oncompletionListener or you may try reseting the mediaplayer after one audio is completed. example code here. You may also use session id to keep track of which file was playing and which to start now.
mPlayer = new MediaPlayer();
//set other attributes here
mPlayer.setAudioSessionId(1);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
//check which audio session was playing and set new datasource and session
mPlayer.reset();
//set other data source here and different session id
}
});
Hope it solves your problem.
If you want to play sound in order, try this:
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp2.start();
}
});
mp2.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp3.start();
}
});
mp3.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp1.start();
}
});

Lag between two sound "Mediaplayer" android

I want to play two sounds. I'm using this code but there's lag between the two sounds for about 2s. I want to play the second directly when the first sound is finished. How can I do that?
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.s83);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp2){
mp2 = MediaPlayer.create(getApplicationContext(),R.raw.ss);
mp2.start();
}
});
Android 4.1 (v16) adds MediaPlayer.setNextMediaPlayer which was added to allow gapless playing as per the feature list. Prior to that, there is always a small delay between onCompletion and starting another MediaPlayer due to buffering. Creating the second MediaPlayer before onCompletion might help as well (and would be required for using setNextMediaPlayer anyways).
Try this:
Make mp2 a MediaPlayer class field and then use this code:
mp2 = MediaPlayer.create(getApplicationContext(),R.raw.ss);
MediaPlayer mp = MediaPlayer.create(getApplicationContext(),R.raw.s83);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp){
YourActivityClass.this.mp2.start();
}
});
It will at least create the second MediaPlayer object and prepare() before the first sound is played, so you gain some time there.

Play sound file on Timer

I am displaying a Timer on a screen.
The requirement is I need to play a sound every second when the timer duration is 5 seconds remaining & finally when it reaches 0 (end) , I need to play another sound.
I am using the following code:
public void onTick(long millisUntilFinished) {
long timeLeft = secondsRemaining // I am getting the seconds left here
if (timeLeft <= 5) {
playAlertSound(R.raw.beep);
}
else if(timeLeft == 0){
playAlertSound(R.raw.beep1);
}
public void playAlertSound(int sound) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
The problem I am facing using the above code , though the each beep sound duration is less than 1 second , I am getting a continuation of the sound. I want to have separate beep sound for every second starting from the remaining 5 seconds till it reaches zero.
Also , the volume of the sound is too low.
Kindly provide your inputs , whether I need to follow any other logic ?
Thanks in advance.
Warm Regards,
CB
You should make sure looping is disabled using the setLooping function and for the volume issue, you can set the playback volume using: the setVolume function.
But, for your appliction you may want to look into using the SoundPool class instead of the MediaPlayer because it's more suitable for situations like yours when you want to play the same short sound more than once.
With your sample code, I would also reverse the order of the setting the onCompleteListener and starting playback, like this:
MediaPlayer mp = MediaPlayer.create(getBaseContext(), sound);
// move the mp.start() from here..
mp.setOnCompletionListener(new OnCompletionListener() {
// your handler logic here..
});
// and add the setLooping and setVolume calls here..
mp.setLooping(false);
mp.setVolume(1.0, 1.0);
mp.start(); // to here..
I tried this code without the OnCompletionListener, and it worked fine with no looping? I guess I'm not understanding your problem. Also... setVolume(1.0,1.0) isn't correct, you need (float,float) not doubles.

Categories

Resources