I use below code to play audio in background:
String[] Path = new String[] {path1, path2, ...};
mMediaPlayer.setDataSource(Path[i]);
mMediaPlayer.prepare();
mMediaPlayer.start();
mMediaPlayer.seekTo(0);
While I play the first one Path[0] in background.
I want to make it auto play next one Path[1] after Path[0] play finish, how to arrive it?
You should override onCompletionListener like this,
mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Log.i("Completion Listener","Song Complete");
mp.stop();
mp.reset();
mp.setDataSource([nextElement]);
mp.prepare();
mp.start();
}
});
If you use a onPreparedListener in your MediaPlayer then you cal also use the prepareAsync command and ignore the .start().
You need to set an OnCompletionListener to your MediaPlayer, in the listener set the source to path2, prepare and play.
http://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html
Related
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();
}
});
i try to stream and play an audio file in my application. i use this code :
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(url);
player.prepare();
player.start();
i want to know is it possible to detect when player finished playing ? i want to change a button color after player finished playing
Check out the setOnCompletionListener method:
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
Log.d(TAG, "Done playing!");
}
});
You can actually set a ton of callbacks here. In addition to setOnCompletionListener, we have:
setOnBufferingUpdateListener
setOnErrorListener
setOnInfoListener
setOnPreparedListener
setOnSeekCompleteListener
setOnTimedMetaDataAvailableListener
setOnTimedTextListener
setOnVideoSizeChangedListener.
I'm using the following code to play a mp3 from raw folder but nothing happens!
can anyone help me? thanks
MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(ShapesActivity.this, R.raw.circle);
mp.start();
mp.release();
As pointed out #TuomasK you release the media before playing it. You should implement OnCompletionListener to release MediaPlayer properly.
You can do it like this:
MediaPlayer mMp = MediaPlayer.create(ShapesActivity.this, R.raw.circle);
mMp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.reset();
mp.release();
};
});
You are releasing the mediaplayer right after you start it, so the sound doesn't play. You need to remove mp.release()
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.
I have a problem on MediaPlayer's method : create().
(Version 2.3.3)
mMediaPlayer = new MediaPlayer();
mMediaPlayer.create(this, musicIds[0]).start();
First line is Ok, but the second line is the point.
The eclipse suggestions me "Change access to static using 'MediaPlayer'(declaring type)" and "Add #SuppressWarnings 'static-access' to on Create()".
By the way, I take eclipse's suggestions........but still don't work.
It may be suggesting you do something like this:
MediaPlayer mMediaPlayer = MediaPlayer.create(this, musicIds[0])
mMediaPlayer.start();
MediaPlayer mp = MediaPlayer.create(this, musicIds[0]);
mp.start()
On success, prepare() will already have been called and must not be called again.
Don't forget to call mp.release() after you are done with it.
I don't think the Eclipse's suggestions should be the subject for not working your code.
I will concentrate my attention on the way how the MediaPlayer is started. Beware that it may take some time until the song is initialized. So you better set the onPrepared() listener and call start() only when the mediaplayer is prepared.
mMediaPlayer.create(this, musicIds[0])
mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mMediaPlayer.start();
}
});
This is a more complete example of how to wait for the media player to be ready to play and ensure that you are releasing the media player. Without calling mediaPlayer.release() you may have problems on subsequent calls to the MediaPlayer.
MediaPlayer mediaPlayer= MediaPlayer.create(context.getApplicationContext(), R.raw.notification_ring);
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
}
});
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
mediaPlayer.start();
}
});