Android: How To Stop Media Player Onclick Of A Button
I have a play and stop button. When I run the following codes, Play button goes well. But when I click the stop button, I'm getting a force close message. I'm new to android dev. M
Solution 1:
First, try without:
mHandler.post(new Runnable(){
publicvoidrun(){
customTextBG.setText("Nicholas was… \n\nOlder than sin, ");
customTextHL.setText("");
}
});
and code for stop player:
if(mPlayer.isPlaying())
{
mPlayer.stop();
}
and second, can you provide your xmlfiles, then we can test it and see the errors.
Solution 2:
Put a try catch block like this -
try {
mPlayer.stop();
mPlayer.release();
} catch(Exception ex) {
ex.printStackTrace()
}
If your device is set to debug applications put a breakpoint in the catch block and let us know what the exception says. You can also use the emulator to debug.
Solution 3:
The Above code is modified as following code and gets output as per your requirement.When you are create a mediaplayer throuh MediaPlayer.create(....) Method ,no need to prepare again it. Once release or reset Methods are used,they should be required to re-intialize the datasource.
publicclassHelloMediaextendsActivity {
MediaPlayer mPlayer;
/** Called when the activity is first created. */@OverridepublicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPlayer = MediaPlayer.create(HelloMedia .this, R.raw.sleepaway);
setContentView(R.layout.main);
finalHandlermHandler=newHandler();
finalTextViewcustomTextBG= (TextView) findViewById(R.id.customTextBG);
finalTextViewcustomTextHL= (TextView) findViewById(R.id.customTextHL);
customTextBG.setText("Nicholas was… \n\nOlder than sin, ");
customTextHL.setText("");
final String words[] = {
"Nicholas ", // 0"was... \n\n", // 1"Older ", // 2"than ", // 3"sin, ", // 4
};
finallong startEndTime[][]={
{ //start time1148,// 0,01826, // 0,12766,// 0,23079,// 0,33549,// 0,4
},
{ //end time1357,// 1,02192, // 1,13027,// 1,23183,// 1,33966,// 1,4
}
};
customTextBG.setText("Nicholas was… \n\nOlder than sin, ");
customTextHL.setText("");
View.OnClickListenerhandler=newView.OnClickListener(){
publicvoidonClick(View v) {
switch (v.getId()) {
case R.id.widget30: // PLAYtry{
if(mPlayer==null)
mPlayer = MediaPlayer.create(HelloMedia .this, R.raw.sleepaway);
if( !mPlayer.isPlaying() )
{
if(!mPlayer.isPlaying())
mPlayer.start();
mHandler.post(newRunnable(){
publicvoidrun(){
if(mPlayer!=null)
if(mPlayer.isPlaying()){
finallongcurrentPos= mPlayer.getCurrentPosition();
intx=0;
while( x < 5){
if( currentPos > startEndTime[0][x] && currentPos < startEndTime[1][x] ){//0
customTextHL.append(words[x]);
words[x]="";
}
x++;
} mHandler.postDelayed(this, 1);
}
}});
}
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
break;
case R.id.widget31: // RESET
mHandler.post(newRunnable(){
publicvoidrun(){
customTextBG.setText("Nicholas was… \n\nOlder than sin, ");
customTextHL.setText("");
}
});
try{
if(mPlayer.isPlaying())
{
mPlayer.stop();
mPlayer.reset();
mPlayer=null;
}
}catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
break;
}
}
};
// BUTTONS
findViewById(R.id.widget30).setOnClickListener(handler); // PLAY
findViewById(R.id.widget31).setOnClickListener(handler); // RESET
}
}
Solution 4:
Here's my code, tested and working fine:
package com.example.com.mak.mediaplayer;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.app.Activity;
publicclassMainActivityextendsActivity {
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mpp;
mpp=MediaPlayer.create(this,R.raw.red); //mp3 file in res/raw folder
Button btnplay=(Button)findViewById(R.id.btnplay); //Play
btnplay.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View vone) {
mpp.start();}
});
Button btnpause=(Button)findViewById(R.id.btnpause); //Pause
btnpause.setOnClickListener(newView.OnClickListener() {
@OverridepublicvoidonClick(View vtwo) {
if(mpp.isPlaying())
{
mpp.pause();
mpp.seekTo(0);
}
}
});
}
}
Solution 5:
if(mPlayer != null){
mPlayer.release();
}
Post a Comment for "Android: How To Stop Media Player Onclick Of A Button"