问题
I'm trying to add background music to my application. All I want is the sound should played on pressing the btnoptn
Button
and its text transitions into the "music off". The music should continue on any Activity
until the settings page is returned to and the same Button
is pressed again. The music then stops and the Button
text changes to "music on".
This my code so far:
package hello.english;
import hello.english.R;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.media.MediaPlayer;
import android.os.Bundle;
public class welcome extends Activity implements OnClickListener{
private Button btnoptn;
private Button btnmenu;
public static MediaPlayer mp2;
private void btnoptn(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Button testButton = (Button) findViewById(R.id.btnoptn);
testButton.setTag(1);
testButton.setText("Musik Of");
mp2=MediaPlayer.create(this, R.raw.guitar_music);
mp2.start();
mp2.setLooping(true);
testButton.setOnClickListener( new View.OnClickListener() {
public void onClick (View v) {
final int status =(Integer) v.getTag();
if(status == 1) {
mp2.start();
mp2.setLooping(true);
testButton.setText("Musik Of");
v.setTag(0); //pause
} else {
mp2.pause();
testButton.setText("Musik On");
v.setTag(1);
} //pause
}
});
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
btnoptn=(Button)findViewById(R.id.btnoptn);
btnmenu=(Button)findViewById(R.id.btnmenu);
btnoptn.setOnClickListener( new View.OnClickListener() {
public void onClick(View view) {
btnoptn(null);
}
});
btnmenu.setOnClickListener( new View.OnClickListener() {
public void onClick(View view2) {
btnmenu();
}
});
}
protected void btnmenu() {
try {
Intent btnmenu= new Intent (this, menuenglish.class);
startActivity(btnmenu);
} catch (Exception e) {
e.printStackTrace();
}
}
public void onStart() {
super.onStart();
btnoptn.setTag(0);
}
public void onClick(View view2) {
// TODO Auto-generated method stub
}
}
回答1:
This is a really simple example. I don't know how it works if you switch between Activities, i never really worked with the MediaPlayer class.
public class MainActivity extends Activity
{
private MediaPlayer mediaPlayer;
private Button musicButton;
private OnClickListener listener = new OnClickListener()
{
// a boolean value in the names Onclicklistener class.
// this will help us to know, if the music is playing or not.
boolean isPlaying = false;
@Override
public void onClick(View arg0)
{
if(isPlaying)
{
mediaPlayer.pause(); //stop the music
musicButton.setText("Start"); //change the buttons text
}
else
{
mediaPlayer.start(); //start the music
musicButton.setText("Stop"); //change the text
}
//changing the boolean value
isPlaying = !isPlaying;
}
};
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Creating the mediaplayer with a desired soundtrack.
mediaPlayer = MediaPlayer.create(this, R.raw.my_music);
//Getting the Button reference from the xml
musicButton = (Button) findViewById(R.id.music_button);
//Setting the listener
musicButton.setOnClickListener(listener);
}
}
来源:https://stackoverflow.com/questions/11806745/how-to-create-button-to-control-background-sound