How to play the audio again after completing the audio? - android

I am facing the issue in playing the same audio after completing the audio. In my case i am using the play and pause function in a single button. When mediaplayer is playing the pause icon is visible ,when the audio is paused the play icon is visible.My problem is after audio is finished palying the audio, the mediaplayer seekbar should be in starting point of the audio and play button should be visible. How can i do this.
I tried mediaPlayer.setLooping(true); but the after completing the audio it starts again but pause icon is visible and audio is playing continiously.
also i tried `
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
// mediaPlayer.seekTo((int) startTime);
stop.setVisibility(View.GONE);
play.setVisibility(View.VISIBLE);
seekBar.setProgress((int) startTime);
}
});
In this method it shows only the mediaplayer completing state.
My Requirement is after completing the audio the mediaplayer should be in initial state and in pause condition. If audio is pause means the play icon should be visible.
In above image while playing the audio the pause image is visible, while in pause condition the play icon will be visible. When the audio is completed the seekbar position should be in starting point and audio should be pause condition.How to do this please help me.

mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
// mediaPlayer.seekTo((int) startTime);
// stop.setVisibility(View.GONE);
// play.setVisibility(View.VISIBLE);
// seekBar.setProgress((int) startTime);
seekBar.setProgress(0);
if (mediaPlayer.isPlaying()){
stop.setVisibility(View.VISIBLE);
play.setVisibility(View.GONE);
}else {
stop.setVisibility(View.GONE);
play.setVisibility(View.VISIBLE);
}
}
});
I hope it's helpful to you ..!

Related

When screen rotated of device then audio player play and pause button not work

In android audio app, when the screen of the device rotates then audio play and pause button do not work. I'm using a seek bar for track the audio duration which also is not working.
mPlayer = MediaPlayer.create(this, R.raw.audio);
public void playAudio(View view){
mPlayer.start();
}
public void pauseAudio(View view){
mPlayer.pause();
}

How play video without stop other audio tracks?

When I playing video in my application, other audio stop playing. Because my video has no sound, so I don't want to stop other audio.
I have tried to mute my video by set volume to 0 but it doesn't help
setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.setVolume(0f, 0f);
}
});
Any help or suggestion would be great appreciated

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.

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();
}
});

Android Media Player Release won't stop playing

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?

Categories

Resources