2016-02-23 135 views
0

經過數小時的研究,我無法解決一個簡單的問題。我的小菜技巧是史詩般的。Radio Group的媒體播放器資源

我有收音機組,哪裏是幾首歌,我想播放或停止由於兩個控制按鈕。我可以播放並停止播放,但無法再播放同一首歌曲。在logcat錯誤(-38,0)。我知道我必須再次設置新的源,但我無法管理它。不過,我覺得我可以設置在停止按鈕的新來源,但沒有方法來獲取最後播放的曲目......

@Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      switch (checkedId) { 
        case R.id.rb1: { 
         if (mp.isPlaying()){ 
          mp.stop(); 
          mp = MediaPlayer.create(mContext, R.raw.actionable);} 
         else { 
         mp = MediaPlayer.create(mContext, R.raw.actionable);} 
        } 
        break; 

        case R.id.rb2: { 
         mp = MediaPlayer.create(mContext, R.raw.cute); 

        } 
        break; 

        case R.id.rb3: { 
         mp = MediaPlayer.create(mContext, R.raw.dubstep); 
        } 
        break; 

        case R.id.rb4: { 
         mp = MediaPlayer.create(mContext, R.raw.epic); 
        } 
        break;} 
     } 
    }); 

    ibPlay = (ImageButton) findViewById(R.id.ibPlay); 
    ibPlay.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      mp.start(); 
     } 
    }); 


    ibStop = (ImageButton) findViewById(R.id.ibStop); 
    ibStop.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mp.stop(); 


     } 
    }); 

回答

0

好了,我到底做了這樣的。對不起,打擾你。我永遠不能再醉編程:)

int whatsong = 1; 

final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rg); 
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup group, int checkedId) { 
      if (mp.isPlaying()){ 
       mp.stop(); 
      } 
      switch (checkedId) { 
        case R.id.rb1: { 

         whatsong = 1; 
        } 
        break; 

        case R.id.rb2: { 
         whatsong = 2; 
        } 
        break; 

        case R.id.rb3: { 
         whatsong = 3; 
        } 
        break; 

`

enter code here`ibPlay.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if (mp.isPlaying()){ 
        mp.stop(); 
       } 

       switch (whatsong){ 

        case 1:{mp = MediaPlayer.create(mContext, R.raw.actionable); 
         mp.start();} 
        break; 
        case 2:{mp = MediaPlayer.create(mContext, R.raw.badass); 
         mp.start();} 
        break;