Multiple instance of media player issue in android - android

I am working with MediaPlayer in Android. I have all the list of songs name and URLs of songs from server. when I click on any item a song from server is played in MediaPlayer. Here is my code for playing song on click of "Listview" item.
txtEndTimingForMediaPlayer.setText("");
txtStarTimingForMediaPlayer.setText("");
seekBarPlayer.setProgress(0);
// this code is for stop current playing song and release media player
if(mediaPlayer!=null && mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.reset();
mediaPlayer.release();
mediaPlayer=null;
}
// start new song for play
mediaPlayer=new MediaPlayer();
Uri myUri1 = Uri.parse(url);
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.reset();
mediaPlayer.setDataSource(getActivity(), myUri1);
mediaPlayer.prepareAsync();
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer player) {
playPause=true;
player.start();
mediaFileLengthInMilliseconds = player.getDuration();
}
});
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
playPause=false;
btnPlayPause.setBackgroundResource(R.drawable.icon_play);
}
});
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Now issue with this code is when I click continuously on song, then same song plays multiple time. how to avoid this multiple instance issue.

I think if(mediaPlayer!=null && mediaPlayer.isPlaying()) is not true when you click too fast to play same song and then mediaPlayer=new MediaPlayer(); is creating new instance and play song, which will result in hearing same song multiple times. debug and check if code enter that if (condition)

Related

Adding controls like pause and play to media player

I have been able to play audio from my firebase storage and i want the song to stop and start when a user clicks on same button but i haven't been able to get get the right code for that.
this is what i have tried
MediaPlayer mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(url);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}else {
mediaPlayer.stop();
}
}
});
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
The above code is correct, now do one thing create button listener and write below code
button.setOnClickListener(view.onCLickListeners(){
#override
public void onClick(){
if (!mediaPlayer.isPlaying()) {
mediaPlayer.play();
}
else {
mediaPlayer.pause();
}
}});
And please define mediaplayer globally.

Prevent mediaplayer from calling onCompletionListener

I'm using the following code to play music when an user selects a song from the listview.
if (mMediaPlayer.isPlaying()) {
mMediaPlayer.reset();
try {
mMediaPlayer.setDataSource(MainActivity.localTrackList.get(MainActivity.currentOffset).getPath());
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
} else {
mMediaPlayer = new MediaPlayer();
try {
mMediaPlayer.setDataSource(MainActivity.localTrackList.get(MainActivity.currentOffset).getPath());
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
}
mMediaPlayer.reset(); is calling the onCompletionListener. In my oncompletionListener, I'm playing the next song and hence instead of playing the selected song, it plays the next song in the listview. Is there by any ways I can prevent calling onCompletionListener and play only the song that's selected from listview?
You could do something like this.
boolean isMediaPlayerReset = false;
MediaPlayer mMediaPlayer = null;
//on list item click
if(mMediaPlayer.isPlaying()){
isMediaPlayerReset = true;
mMediaPlayer.reset();
try {
mMediaPlayer.setDataSource("/path");
} catch (IOException e) {
e.printStackTrace();
}
mMediaPlayer.prepareAsync();
}
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
if(isMediaPlayerReset){
isMediaPlayerReset = false;
return;
}
//play next song
}
});

How I can get medial player Duration and stop Chronometer?

I am making a android simple media player for playing a audio files and facing a problem with it.
I want to stop Chronometer and media player
When the current Duration of playing file == total duration of file..?
Here is my demo code.
private void playAudio() {
mediaPlayer = new MediaPlayer();
try {
mediaPlayer.setDataSource(outputFile);
} catch (IOException e) {
e.printStackTrace();
}
try {
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mChronometer.setBase(SystemClock.elapsedRealtime());
mChronometer.start();
mediaPlayer.start();
long totalDuration = mediaPlayer.getDuration();
Log.i("MediaPlayer", String.valueOf(totalDuration));
}
here is another code added from answer .
i add the listrner
mediaPlayer.setOnCompletionListener(CompletionListener);
MediaPlayer.OnCompletionListener CompletionListener = new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
long totalDuration = mp.getDuration();
}
};
Use onCompletionListener to check when media player has reached end of the media file.
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// do your task
}
});
For further reference see this link
Media Player

MediaPlayer and error when playing music from online

mediaPlayer=new MediaPlayer();
mediaPlayer.reset();
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
}
});
try {
mediaPlayer.setDataSource("192.168.191.1/test.mp3");
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.prepareAsync();
above is my code. It works well from such url: http://programmerguru.com/android-tutorial/wp-content/uploads/2013/04/hosannatelugu.mp3
But it don't work when I try to access mp3 file in my server, 192.168.191.1/test.mp3, and I use wamp for my server.
I had the same requirement and it is working on my end :
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource("mp3 link"
);
player.prepare();
player.start();
} catch (Exception e) {
// TODO: handle exception
}

Trigger an event when mediaPlayer stops

I am making an app that uses sound effects. I would like to know how i can detect when a sound has finished playing and trigger an event .
i tried this :
player = new MediaPlayer();
if (player != null) {
player.reset();
player.release();
}
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
sonar.setBackgroundResource(R.drawable.sonar_off);
stopPlay();
}
});
if(state==1){
player = MediaPlayer.create(this, R.raw.sonar_slow);
}else if(state==2){
player = MediaPlayer.create(this, R.raw.sonar_medium);
}else if(state==3){
player = MediaPlayer.create(this, R.raw.sonar_fast);
}
try{
player.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
player.start();
You can set an OnCompleteListener() to the player object. The code for it is:
mPlayer.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp)
{
"your code comes here"
}
});
Here the mPlayer is the object of the MediaPlayer which is currently running.
There are multiple ways to play sounds, assuming you use the MediaPlayer class you could register a MediaPlayer.OnCompletionListener

Categories

Resources