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.
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 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();
}
});
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
Although a seasoned developer, I'm new to Android, and am wanting to develop an app to loop between user defineable sections of audio recorded via Android's mic in.
I'm wanting as low latency as possible when the loop point is hit (i.e. miminal time to seek the start point of the section and resume playback).
I'm after some general recommendations such as:
audio file format
which classes should I be considering for playback (e.g. soundpool, media player etc)
Thanks for any advice
Dave
You can use Media player and an OnCompletetionListener like this:
MediaPlayer mp;
mp = MediaPlayer.create(getApplicationContext(), R.raw.yoursound);
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.create(getApplicationContext(), R.raw.nextsound);
mp.prepare();
mp.start();
}
});
mp.prepare();
mp.start();
As for sound file types I've never noticed much of a difference between any of them. I tend to stick with .mp3 or .ogg though just because the file size tends to be smaller for those than for .wav.
Edit: Ahh I see, If you are wanting to play the whole audio file over and over you just need to call setLooping(true) like this:
MediaPlayer mp;
mp = MediaPlayer.create(getApplicationContext(), R.raw.yoursound);
mp.setLooping(true);
mp.prepare();
mp.start();
If you are trying to play the full audio file once, then loop some small section of it over and over you can use the .seekTo() method inside the completionlistener like this:
MediaPlayer mp;
mp = MediaPlayer.create(getApplicationContext(), R.raw.yoursound);
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp = MediaPlayer.create(getApplicationContext(), R.raw.yoursound);
mp.seekTo(3000); //3 seconds in
mp.prepare();
mp.start();
}
});
mp.prepare();
mp.start();
I have followed the steps to create my media player object but I cant understand why it is not playing the music track. I used the following code:
mp = new MediaPlayer();
mp.create(this, R.raw.testmed);
mp.setVolume(100, 100);
mp.start();
but no sound is playing through the emulator, and furthermore when i check the method mp.isPlaying() it returns false. What have I missed?!
Many thanks
You have to call all methods necessary to actually start the player. Take a look at Android Media Player state diagram
I think you need to prepare the player before starting. When the player is prepared, it can be started. This is done through a onPreparedListener:
mp = new MediaPlayer();
mp.create(this, R.raw.testmed);
mp.setVolume(100, 100);
mp.setOnPreparedListener(this);
mp.prepare();
Then you will need to define this and it should work:
public void onPrepared(MediaPlayer player) {
mp.start();
}