I m making an app for playing audio in which I have made play, pause and reset button.All button are working when the app starts, if I play the audio and reset it, after pressing the reset button , if I again press the play button, the audio does not play.
package com.example.android.mediaplayer;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
private MediaPlayer mmediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mmediaPlayer = MediaPlayer.create(this, R.raw.song);
Button b1=(Button) findViewById(R.id.play_button);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Playing sound",Toast.LENGTH_SHORT).show();
mmediaPlayer.start();
mmediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
Toast.makeText(getApplicationContext(),"Song is done",Toast.LENGTH_SHORT).show();
}
});
}
});
Button b2=(Button) findViewById(R.id.pause_button);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mmediaPlayer.pause();
}
});
Button b3=(Button) findViewById(R.id.reset_button);
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mmediaPlayer.reset();
}
});
}
}
After you call mediaPlayer.reset(), you then must also call the prepare method.
You can also to do this instead: mediaPlayer.seekTo(0); mediaPlayer.pause();
Then you can call play again to start music from 0.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With