mp.stop() problem
I am new to android,actually this is my second program.I am programming from last night and i am trying to find a solution for hours!!!I read everything here and i google my problem but i still cant fix it!! My problem is the usual as i understood that i cannot stop audio playback when i click another button.I tried mp.release,stop,everything but keeps crashing when i press stop button or back!!What i am doing wrong??I have 22 buttons and i play 21 songs(one button at top for stop). Also i forgot to mention that i tried to make button when clicking 2nd time to stop playing but i couldn't do it that's why i put stop button.
this is my code
package org.kidsongs;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.Button;
public class menu extends Activity {
public MediaPlayer mp=null;
Button playerButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
final Button button = (Button) findViewById(R.id.Button01);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song1);
mp.start();
}
});
final Button button2 = (Button) findViewById(R.id.Button02);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song2);
mp.start();
}
});
final Button button3 = (Button) findViewById(R.id.Button03);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song3);
mp.start();
}
});
final Button button4 = (Button) findViewById(R.id.Button04);
button4.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song4);
mp.start();
}
});
final Button button5 = (Button) findViewById(R.id.Button05);
button5.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song5);
mp.start();
}
});
final Button button6 = (Button) findViewById(R.id.Button06);
button6.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song6);
mp.start();
}
});
final Button button7 = (Button) findViewById(R.id.Button07);
button7.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song7);
mp.start();
}
});
final Button button8 = (Button) findViewById(R.id.Button08);
button8.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song8);
mp.start();
}
});
final Button button9 = (Button) findViewById(R.id.Button09);
button9.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song9);
mp.start();
}
});
final Button button10 = (Button) findViewById(R.id.Button10);
button10.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song10);
mp.start();
}
});
final Button button11 = (Button) findViewById(R.id.Button11);
button11.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song11);
mp.start();
}
});
final Button button12 = (Button) findViewById(R.id.Button12);
button12.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song12);
mp.start();
}
});
final Button button13 = (Button) findViewById(R.id.Button13);
button13.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song13);
mp.start();
}
});
final Button button14 = (Button) findViewById(R.id.Button14);
button14.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song14);
mp.start();
}
});
final Button button15 = (Button) findViewById(R.id.Button15);
button15.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song15);
mp.start();
}
});
final Button button16 = (Button) findViewById(R.id.Button16);
button16.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song16);
mp.start();
}
});
final Button button17 = (Button) findViewById(R.id.Button17);
button17.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song17);
mp.start();
}
});
final Button button18 = (Button) findViewById(R.id.Button18);
button18.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song18);
mp.start();
}
});
final Button button19 = (Button) findViewById(R.id.Button19);
button19.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song19);
mp.start();
}
});
final Button button20 = (Button) findViewById(R.id.Button20);
button20.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song20);
mp.start();
}
});
final Button button21 = (Button) findViewById(R.id.Button21);
button21.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song21);
mp.star开发者_如何学编程t();
}
});
final Button stopbutton = (Button) findViewById(R.id.Button22);
stopbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp.stop();
mp.release();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.info, menu);
return true;
}
@Override
protected void onPause() {
super.onPause();
if(mp.isPlaying()){
mp.stop();
}
}
@Override
public void onDestroy(){
super.onDestroy();
mp.release();
}
}
I believe
public void onClick(View v)
{
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song21);
mp.start();
}
should be
public void onClick(View v)
{
mp = MediaPlayer.create(getBaseContext(),R.raw.song21);
mp.start();
}
The code you have posted is wrong, you are creating local MediaPlayer objects in each click handler. Use the object declared as a member of the activity instead, and then, when you call the stop method the reference will be established correctly.
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.song1);
mp.start();
}
});
should be:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mp = MediaPlayer.create(getBaseContext(),R.raw.song1);
mp.start();
}
});
精彩评论