0
我试图播放在整个应用程序中运行的背景声音。在这里,我有3个活动,并在主要活动启动时开始发声。 我想要实现这些: 1)在整个应用程序中连续播放bg声音,无论任何活动加载。
2)当用户点击声音关闭按钮时关闭声音。 3)当应用程序关闭时停止声音。在Android中播放背景声音
到目前为止,我已经试过这段代码来启动声音,但即使应用程序已关闭,它仍会继续播放。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
audioPlayer();
}
boolean isPlayingSound = true;
public void onClickSound(View view) {
final Button btn1 = (Button) findViewById(R.id.button3);
if(isPlayingSound){
btn1.setBackgroundResource(R.drawable.sound00);
isPlayingSound=false;
audioPlayer(false);/*Sound doesn't stops here*/
}
else{
btn1.setBackgroundResource(R.drawable.sound11);
isPlayingSound=true;
audioPlayer(true);
}
}
public void audioPlayer(boolean status){
MediaPlayer mp = MediaPlayer.create(this, R.raw.bg);
if(status) {
mp.start();
}
else {
mp.stop();
}
}
任何人都可以看看,并帮我在这里。感谢帮助!!
谢谢。我从http://marakana.com/forums/android/examples/60.html获得详细信息 – Amrit