我试图做10个按钮,这将播放一些音乐点击和当用户点击后退按钮,他应该退出应用程序和音乐应停止活动。所有我tryied没有做任何事情。onDestroy停止mediaplayer
如果有人可以建议我的东西,这是代码:
package app.technozed.yearstest;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ToggleButton;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ToggleButton unu = (ToggleButton)this.findViewById(R.id.button1);
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.tt);
ToggleButton doi = (ToggleButton)this.findViewById(R.id.button2);
final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.rr);
ToggleButton trei = (ToggleButton)this.findViewById(R.id.button3);
final MediaPlayer mp3 = MediaPlayer.create(this, R.raw.dd);
ToggleButton patru = (ToggleButton)this.findViewById(R.id.button4);
final MediaPlayer mp4 = MediaPlayer.create(this, R.raw.vv);
ToggleButton cinci = (ToggleButton)this.findViewById(R.id.button5);
final MediaPlayer mp5 = MediaPlayer.create(this, R.raw.ss);
unu.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If the music is playing
if(mp1.isPlaying()) {
// Pause the music player
mp1.setLooping(false);
mp1.pause(); }
// If it's not playing
else
{
// Resume the music player
mp1.setLooping(true);
mp1.start(); }
}
});
doi.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If the music is playing
if(mp2.isPlaying() == true) {
mp2.setLooping(false);
// Pause the music player
mp2.pause(); }
// If it's not playing
else {
// Resume the music player
mp2.setLooping(true);
mp2.start(); }
}
});
trei.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If the music is playing
if(mp3.isPlaying()) {
mp3.setLooping(false);
// Pause the music player
mp3.pause(); }
// If it's not playing
else {
// Resume the music player
mp3.setLooping(true);
mp3.start(); }
}
});
patru.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If the music is playing
if(mp4.isPlaying()) {
mp4.setLooping(false);
// Pause the music player
mp4.pause(); }
// If it's not playing
else {
// Resume the music player
mp4.setLooping(true);
mp4.start(); }
}
});
cinci.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// If the music is playing
if(mp5.isPlaying()) {
mp5.setLooping(false);
// Pause the music player
mp5.pause(); }
// If it's not playing
else {
// Resume the music player
mp5.setLooping(true);
mp5.start(); }
}
});
}
@Override
protected void onDestroy()
{
super.onDestroy();
MediaPlayer mp = new MediaPlayer();
if (mp.isPlaying()) { mp.stop(); mp.release();
//finally
}
}
我的理解是,在部分的onDestroy我做错了,但我怎么能释放MP1,MP2,MP3,。 ...?
好的,我做了你说的话,声音停在了Destroy上,但它并没有停止在最小化。我怎样才能阻止它? –
只需在您的onPause方法中调用相同的代码即可。但也从你的onDestroy中删除它。 – JanBo
只是做了这个。我用onPause改变了onDestroy,它在最小化时起作用。 ' \t @覆盖 \t保护无效的onPause() \t { \t super.onDestroy(); \t mp1.stop(); \t mp2.stop(); \t mp3.stop(); \t mp4.stop(); \t mp5.stop(); \t} ' 但是,当最大化声音不想玩,我怎么才能添加onResume选项来恢复只有在哪里播放的声音? –