关于MediaPlayer这个类的问题
我设计了4个RaidoButton 然后对4个 进行监听 然后一选择后播放 地区方言 以前没用过 MediaPlayer
MediaPlayer 是否可以这么写
虽然可以做到 播放下一个音效 然后停止上一个音效 但是
问题来了 如果播放完整的音效 再选择的时候 却报错了.
public void onClick(View v) { // TODO Auto-generated method stub String media = titls; switch (v.getId()) { case R.id.radioButton1: if (rbtPt.isChecked()) { rbtPt.setChecked(true); rbtGd.setChecked(false); rbtSc.setChecked(false); rbtKa.setChecked(false); if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.reset(); mediaPlayer.release(); } if (media.equals("开会")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.meeting_tw); } else if (media.equals("开车")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.car_tw); } else if (media.equals("出差")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.travel_tw); } else if (media.equals("培训")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.train_tw); } else if (media.equals("手机没电")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.nobattery_tw); } else if (media.equals("运动")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.sport_tw); } else if (media.equals("不方便接听")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.nocall_tw); } mediaPlayer.start(); } break; case R.id.radioButton2: if (rbtGd.isChecked()) { rbtPt.setChecked(false); rbtGd.setChecked(true); rbtSc.setChecked(false); rbtKa.setChecked(false); if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.reset(); mediaPlayer.release(); } if (media.equals("开会")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.meeting_yy); } else if (media.equals("开车")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.car_yy); } else if (media.equals("出差")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.travel_yy); } else if (media.equals("培训")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.train_yy); } else if (media.equals("手机没电")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.nobattery_yy); } else if (media.equals("运动")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.sport_yy); } else if (media.equals("不方便接听")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.nocall_yy); } mediaPlayer.start(); } break; case R.id.radioButton3: if (rbtSc.isChecked()) { rbtPt.setChecked(false); rbtGd.setChecked(false); rbtSc.setChecked(true); rbtKa.setChecked(false); if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.reset(); mediaPlayer.release(); } if (media.equals("开会")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.meeting_sc); } else if (media.equals("开车")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.car_sc); } else if (media.equals("出差")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.travel_sc); } else if (media.equals("培训")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.train_sc); } else if (media.equals("手机没电")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.nobattery_sc); } else if (media.equals("运动")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.sport_sc); } else if (media.equals("不方便接听")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.nocall_sc); } mediaPlayer.start(); } break; case R.id.radioButton4: if (rbtKa.isChecked()) { rbtPt.setChecked(false); rbtGd.setChecked(false); rbtSc.setChecked(false); rbtKa.setChecked(true); if (mediaPlayer.isPlaying()) { mediaPlayer.stop(); mediaPlayer.reset(); mediaPlayer.release(); } if (media.equals("开会")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.meeting_db); } else if (media.equals("开车")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.car_db); } else if (media.equals("出差")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.travel_db); } else if (media.equals("培训")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.train_db); } else if (media.equals("手机没电")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.nobattery_db); } else if (media.equals("运动")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.sport_db); } else if (media.equals("不方便接听")) { mediaPlayer = MediaPlayer.create(Scenese.this, R.drawable.nocall_db); } mediaPlayer.start(); } break; case R.id.back4: finish(); break; case R.id.but_ci_intent: break; case R.id.but_ci_del: finish(); break; } mediaPlayer.setOnCompletionListener(new OnCompletionListener() { public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub try { mp.release(); } catch (Exception e) { e.printStackTrace(); } } }); mediaPlayer.setOnErrorListener(new OnErrorListener() { public boolean onError(MediaPlayer mp, int what, int extra) { // TODO Auto-generated method stub try { mp.release(); } catch (Exception e) { e.printStackTrace(); } return false; } });