I am making an app that involves sound. There is a replay button. All I want is, whenever that is hit, my song whenever finished or in play, to be played from the beginning. I have tried many thing like
mp.stop();
mp.start();
or
mp.stop();
mp.prepare();
mp.start();
but none of them worked. Can you help me?
try to seek player to 0 before you stop it:
mp.seekTo(0);
mp.stop();
mp.start();
Related
When I use mediaplayer on click button, after several clicks the sound disappears. When I use soundpool, the time is too short to play the effect sound. Can anybody please help me with this?
As I understood your audio is 20 s. So you can use this code on your button click:
if (mediaPlayer != null){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
mediaPlayer = MediaPlayer.create(this, R.raw.audio_file);
mediaPlayer.start();
The first if statement is to take care that it will keep playing how much ever you clicked the button.
I was using SoundPoolto play sound effects but now I need to switch to MediaPlayer as I need to listen to the onCompletion event to trigger a GUI change.
My questions are:
The sound files are very small, less than 30kB, Can I use
MediaPlayer in the main thread?
Can I just call mediaPlayer.stop() after the play is done like so,
mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
//Do some GUI changes
mediaPlayer.stop();
mediaPlayer.release();
}
});
Then to play another effect will do this:
mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start();
So, is it ok to create and release every time I play an effect?
Initialize your MediaPlayer
MediaPlayer mediaPlayer = new MediaPlayer();
AssetFileDescriptor afd = context.getResources().openRawResourceFd(resid);
mediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength());
afd.close();
Look at the MediaPlayer lifecycle:
When the track is complete the MediaPlayer is in Stopped state and you wan't to change the track, so:
call reset()' to get toIdlethensetDataSourcethenprepareand finallystart()`.
The sound files are very small, less than 30kB, Can I use MediaPlayer in the main thread?
Irrespective of the size of your audio file, you should run MediaPlayer on its own thread, especially if you are playing a file from network. Not doing so may cause ANR (In your case I don't think much to worry about)
Can I just call mediaPlayer.stop() after the play is done
There is no need to release the MediaPlayer after every audio file. Release it once you are no longer going to use it.
how can I load a new sound
AssetFileDescriptor audio = context.getResources().openRawResourceFd(R.raw.next_audio);
mediaPlayer.setDataSource(audio);
I want to loop a music in my app so I use the following code:
mediaPlayer = MediaPlayer.create(this, R.raw.music);
mediaPlayer.setVolume(8f, 8f);
mediaPlayer.start();
mediaPlayer.setLooping(true);
I've try to start and then setLooping, the problem still there.
mediaPlayer = MediaPlayer.create(this, R.raw.music);
mediaPlayer.setVolume(8f, 8f);
mediaPlayer.setLooping(true);
mediaPlayer.start();
But when the music ends it don't restart and my app slows rapidly until total freeze, but there is no crash.
If I look at the log there is a huge succession of :
MediaPlayer_Java: MEDIA_PAUSED
MediaPlayer_Java: MEDIA_STARTED
My phone is a Xperia M4 Aqua.
Thank you for your help !
mediaPlayer.setLooping(true);
is before start media player
In my android application I only play a looping sound with MediaPlayer. I'm trying to loop fluid, eliminating the time between the end of the track and the beginning of the next reading.
I tried with the function setLooping but there is a time between sounds.
I tried with setOnCompletionListener function like this:
MP.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
MP.start();
}
});
However, there are still a few seconds between sounds.
Apparently it is not possible with MediaPlayer; there's an open bug asking for gapless playback that has been open since 2009.
Android: MediaPlayer gapless or seamless Video Playing also has several non-answers on the topic.
I am working on video player in android.when i created surface holder in on create method ,it did not create.but when i created the surface holder in button onclick method,its created
My coding is,
preview=(VideoView)findViewById(R.id.surface);
preview.setEnabled(true);
preview.bringToFront();
holder=preview.getHolder();
holder.setFixedSize(400, 400);
mp=new MediaPlayer();
mp.setDataSource("path");
mp.setDisplay(holder);
mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
mp.setOnBufferingUpdateListener(playerActivity.this);
mp.setOnPreparedListener(playerActivity.this);
mp.prepare();
mp.start();
mp.prepare();
mp.start();
prepare() is asynchronous, which means, it might not be finished when you already call the mp.start. What do you mean by 'surface not created'? Do you just mean the video doesn't play?
Anyway, you should use an MediaPlayer.OnPreparedListener and start the media in onPrepared().