My code to start mediaplayer:
MediaPlayer player = MediaPlayer.create(context, Uri.fromFile(new File(context.getExternalCacheDir().getAbsolutePath()+ File.separator + "test.mp3")));//MediaPlayer.create(context, resId);
player.setAudioStreamType(playOnStream);
player.start();
player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
if after this I start any animation (I have different animations with different views started one after another by listening onAnimationEnd) mediaplayer stops with logcat message:
05-08 11:17:43.180: W/MediaPlayer-JNI(6571): MediaPlayer finalized without being released
Currently I have no ideas about how workaround this
Make sure to release the media player object in onPause() and prepare it again in onResume()
Related
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();
}
});
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.
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?
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();
}
});