Stop button is not working - android

This is a code for play and stop mediaplayer
It made music successfully and played that.But stop button didn't work
Why it's not working?
Please lead me to the correct code
Thanks
mp = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Titraj/"+name+".mp3"));
play.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(mp.isPlaying()){
if(mp!=null){
mp.pause();
play.setImageResource(R.drawable.play);
}
}else{
if(mp!=null){
mp.start();
play.setImageResource(R.drawable.pa);
}
}
}
});
stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(mp.isPlaying()){
if(mp!=null){
mp.stop();
}
}
}
});
mp.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer arg0) {
play.setImageResource(R.drawable.play);
}
});

Use this one....
stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if (mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
});
Also must check you haven't set android:focusable = false for your button in xml file.

Try this in your on stop method
mp.stop();
mp.release();
mp= null;

mp = MediaPlayer.create(this, Uri.parse(Environment.getExternalStorageDirectory().getPath()+ "/Titraj/"+name+".mp3"));
play.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(mp.isPlaying()){
if(mp!=null){
mp.pause();
play.setImageResource(R.drawable.play);
}
}else{
if(mp!=null){
mp.start();
play.setImageResource(R.drawable.pa);
}
}
}
});
stop.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
if(mp.isPlaying()){
mp.stop();
}
}
});
mp.setOnCompletionListener(new OnCompletionListener(){
#Override
public void onCompletion(MediaPlayer arg0) {
play.setImageResource(R.drawable.play);
}
});
Check this code i hop it working....

Related

Code to make a single MediaPlayer play more than one piece of sound in android studio

Being a novice coder, trying to code an app that plays a sound on buttonclick in a fragment; successfully did it but I want to add more small pieces of sounds to play one after another. How can I add those sounds(sound2, sound3, sound4, etc) after that 'R.raw.sound1'? Please guide..
buttonPlayAudioVar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mp != null) {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
}
mp = MediaPlayer.create(getActivity().getApplicationContext(), R.raw.sound1);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
}
});
}
});
//you can add all the songs in a array
ArrayList<String> songsArray = new ArrayList<>();
int position = 0;
buttonPlayAudioVar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediaPlayer(songsArray, position);
}
});
private void mediaPlayer(ArrayList<String> songsArray) {
if (mp != null) {
if (mp.isPlaying()) {
mp.stop();
mp.release();
}
}
if (position > songsArray.size() - 1) {
return;
}
try {
mp.setDataSource(songsArray.get(position));
position++;
mp.prepareAsync();
} catch (IOException e) {
e.printStackTrace();
}
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mp.release();
mp = null;
mediaPlayer(songsArray, position);
}
});
}

how to use media player in dialog alert button

I try to play media player in dialog alert button, somehow it doesn't work but there is no error notification, the dialog works perfectly, and the cancel button the, but for the media player doesn't work
public void TampilanDoa() {
tampilan = new Dialog(DoaHarianActivity.this);
tampilan.setContentView(R.layout.layout_doa);
dengar = tampilan.findViewById(R.id.btdengar);
kembali = tampilan.findViewById(R.id.btkembali);
final MediaPlayer
mp=MediaPlayer.create(this, R.raw.splash);
dengar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mp) {
mp.start();
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
});
kembali.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tampilan.cancel();
}
});
tampilan.show();
}
}

Android audio play and stop

this is the method am using..and will call whenever required...in my case i want to control that audio by one button whether it may toggle or button...
click to start and stop in one button
private void playSound() {
if (isFlashLightOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});mp.start(); playing=true;
}
An edited version of your code
private void playSound() {
if (isFlashLightOn) {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_off);
} else {
mp = MediaPlayer.create(MainActivity.this, R.raw.light_switch_on);
}
mp.prepareAsync(); //prepares the MediaPlayer
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
//MediaPlayer instance prepared
mp.start(); //play the content
playing=true;//update the flag
}
});
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
mp.release(); //playback finished
playing=false; //update the flag
}
});
}
more info on MediaPlayer here
Onclick pause:
public void pause(View view) {
Toast.makeText(getApplicationContext(),
"Pausando..", Toast.LENGTH_SHORT)
.show();
if (mp != null && mp.isPlaying()) {
mp.pause();
}
}//pause
Onclik stop-
public void stop(View view) {
Toast.makeText(getApplicationContext(),
"Stop", Toast.LENGTH_SHORT)
.show();
if (mp != null) {
mp.stop();
}//if
}//stop

How do I program on/off button for Android?

i'm trying to make an on and off button for my background music. music starts right away. music turns off when i press off button but when i press on button it force closes. please help
mp=MediaPlayer.create(this, R.raw.islandsong);
mp.setLooping(true);
mp.start();
onButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mp==null){
mp.start();
}
}
});
offButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (mp!=null){
mp.stop();
mp.release();
mp = null;
}
}
});
You are using the wrong approach. You should check if the music player is playing, if so stop it.
try {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
} catch(Exception ex) {
ex.printStackTrace()
}
Update
You need to edit your code to following if you want to play and stop the Media Player
onButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (!mp.isPlaying()){
mp.start();
}
}
});
offButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
if(mp.isPlaying()){
mp.stop();
mp.release();
}
} catch(Exception ex) {
ex.printStackTrace()
}
}
This code will ensure that Media Player play song only if it is not playing and stop only if it is playing.

Call Release and play multiple sound

with this code I want to stop all the sounds that are being executed, and play only the button clicked, but calling the release, the sound becomes null and you can not run the sound again. but if you do not call release give me problem anyway. how can I find a solution to this conundrum?
Code:
public MediaPlayer mediaPlayer = null;
public Boolean playing = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.b1);
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
if(mediaPlayer != null && mediaPlayer.isPlaying()) mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(menu.this, R.raw.b1);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
// TODO Auto-generated method stub
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
playing=true;
};
});
}
});
Button b2 = (Button)findViewById(R.id.b2);
b2.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
if(mediaPlayer != null && mediaPlayer.isPlaying()) mediaPlayer.stop();
mediaPlayer = MediaPlayer.create(getBaseContext(),R.raw.b2);
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
#Override
public void onPrepared(MediaPlayer mediaPlayer) {
// TODO Auto-generated method stub
mediaPlayer.start();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mediaPlayer) {
mediaPlayer.release();
};
});
}
});
}});
I solved by this:
b1.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v){
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
}
mediaPlayer = MediaPlayer.create(menu.this, R.raw.b1);
mediaPlayer.start();
}});

Categories

Resources