我尝试使用10个按钮进行活动,这些按钮将在点击时播放一些音乐,当用户点按后退按钮时,他应该从应用程序退出并且音乐应该停止。我所做的一切都没有做任何事情。
这是我的代码:
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等?
答案 0 :(得分:1)
您应该在类中存储对MediaPlayer
对象的引用,然后可以稍后停止。你现在正在做的是创建一个MediaPlayer
的新实例,它不会播放任何内容并试图阻止它。
public class MainActivity extends Activity {
private MediaPlayer mediaPlayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
...
mediaPlayer = MediaPlayer.create(this, R.raw.whatever);
...
}
@Override
protected void onDestroy() {
...
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
...
}
}
答案 1 :(得分:0)
您需要将MediaPlayers声明为“全局”变量,更具体地说是类字段。像这样:
public class MainActivity extends Activity {
public MediaPlayer mp1, mp2, mp3 .....
在onCreate中实例化它们并将它们放在onDestroy中。
通过这种方式声明它们,您可以使用类方法访问它们。
你的方式是在函数中将它们声明为局部变量,因此它们在其他方法中不存在
答案 2 :(得分:0)
您正在创建一个新的MediaPlayer。你应该停止mp1,mp2,mp3,mp4和mp5。
我个人会在每个onClick上做这样的事情
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(this, //Specified type);
// If the music is playing
if(mp.isPlaying() == true) {
mp.setLooping(false);
// Pause the music player
mp.pause(); }
// If it's not playing
else {
// Resume the music player
mp.setLooping(true);
mp.start(); }
}
这将更容易,因为你可以用一条简单的线来停止比赛。不幸的是,您无法同时播放两个不同的流。