Android Mediaplayer cuts beginning of file - android

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();

Related

playing a mp3 file in android

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()

Lag between two sound "Mediaplayer" android

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.

Android mediaplayer mute

private MediaPlayer mp_file;
I have mediaPlayer to play mp3 files in my project, it works fine...
mp_file.release();
mp_file = MediaPlayer.create(getApplicationContext(), R.raw.zh_02);
mp_file.start();
I want to create (to make it work) a button called mute_on and mute_off.... How can I do it? By clicking this button all mp3 files in project will mute_off...or mute_on
This code worked for me,
MediaPlayer mp = MediaPlayer.create(context, R.raw.sound_file_1);
mp.start();
for Mute
mp.setvolume(0,0);
& Unmute or full volume
mp.setvolume(0,1);
mediaplayer = MediaPlayer.create(context, R.raw.sound);
mediaplayer.start();
mediaplayer.pause();

Android audio looping

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();

Android MediaPlayer issue

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();
}

Categories

Resources