我有一个简单的列表视图,其中包含一些歌曲的名称。当您单击其中一个孩子时,歌曲将播放。但是,我注意到当你在播放时意外地再次点击它时,它会再次开始播放,导致声音重叠。如何设置它以便当您单击一首歌曲然后单击另一首歌曲时,第一首歌曲停止,第二首歌曲开始。如何修改我的代码来完成此任务。任何帮助将不胜感激。
public class TwelveGrammar extends ActionBarActivity {
private ListView lv;
private MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.twelve_grm);
lv = (ListView) findViewById(R.id.list);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (
this, android.R.layout.simple_list_item_1, Exp_list);
lv.setAdapter(arrayAdapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View view, int pos,
long id) {
if (mp.isPlaying()) {
mp.stop();
}
else {
if (pos==0) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1200);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==1) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1201);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==2) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1202);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==3) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1203);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==4) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1204);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==5) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1205);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==6) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1206);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==7) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1207);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==8) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1208);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==9) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1209);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==10) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1210);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==11) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1211);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==12) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1212);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==13) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1213);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==14) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1214);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
if (pos==15) {
mp = MediaPlayer.create(getBaseContext(), R.raw.g_1215);
mp.start();
mp.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
}
答案 0 :(得分:0)
我认为你应该有一个类似boolean isAnotherSongPlaying
的布尔值,如果它是真的,那么应该停止上一首歌并开始新歌。我相信处理媒体播放器的最佳方式是服务。我还不太熟悉,但我已根据用户的操作实现了背景音乐及其控制服务,并且效果很好。
<强> MyService.class 强>
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.IBinder;
public class MyService extends Service implements OnCompletionListener {
MediaPlayer mediaPlayer;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
mediaPlayer = MediaPlayer.create(this, R.raw.music);// raw/s.mp3
mediaPlayer.setLooping(true);
mediaPlayer.setOnCompletionListener(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
}
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
}
mediaPlayer.release();
}
public void onCompletion(MediaPlayer _mediaPlayer) {
stopSelf();
}
}
在我的主要活动中我有一个按钮可根据音乐是否正在播放来启动和停止服务
Intent play;
play = new Intent(getApplicationContext(), MyService.class);
...
sound.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
if(isPlaying){
stopService(play);
isPlaying = false;
sound.setText("Yes");
} else {
startService(play);
isPlaying = true;
sound.setText("No");
}
}
});
在谷歌搜索它你会找到一些很好的教程。希望我能帮到你。