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();
}
Related
I am trying to play an audio file in my actvity.
The file should play in loop but the first time it plays it gets cut in the beginning for almost a second. The other times it plays correctly. I simply do the following. I also tried with prepare and onpreparedlistener but the result is the same. Any Help?
MediaPlayer mp = new MediaPlayer();
mp = MediaPlayer.create(getApplicationContext(),R.raw.audio_test);
mp.setLooping(true);
mp.start();
add the loop function to your code:
mp.setLooping(true);
for the lag are you tried it in an actual devise?
Try this:
MediaPlayer mp;
mp = MediaPlayer.create(getApplicationContext(), R.raw.audio_test);
mp.setLooping(true);
mp.prepare();
mp.start();
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()
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 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.
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();