首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

关于MediaPlayer这个类的有关问题

2012-03-29 
关于MediaPlayer这个类的问题我设计了4个RaidoButton 然后对4个 进行监听 然后一选择后播放 地区方言 以前

关于MediaPlayer这个类的问题
我设计了4个RaidoButton 然后对4个 进行监听 然后一选择后播放 地区方言 以前没用过 MediaPlayer

MediaPlayer 是否可以这么写

虽然可以做到 播放下一个音效 然后停止上一个音效 但是 

问题来了 如果播放完整的音效 再选择的时候 却报错了.

Java code
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;            }        }); 



[解决办法]
探讨
好的 谢谢了 我试试看 初学android 有的不太懂

热点排行