我的媒体播放器一次被卡住了

时间:2017-05-21 10:41:24

标签: java android audio

我的UI中有两个按钮。这两个按钮分别有两个音频,我已将其称为dk1dk2

我想要的是什么:

  1. 在按下dk1时停止dk2

  2. 要一直重复此过程

  3. 我想创建另外6个音频,例如上面dk1dk2

  4. 问题:我不能不止这样做多次。这是我的代码:

     public class MainActivity extends AppCompatActivity {
    
        //Media Player Loops
    
        //Loops
        MediaPlayer dk1;
        MediaPlayer dk2;   
    
        //Mediaplayer Location
        dk1 = MediaPlayer.create(this, R.raw.dholak01);
        dk2 = MediaPlayer.create(this, R.raw.dholak03);
    }
    
      //Looping and start Media Player
    
      public void playdk1 (View view) {
         if (dk1.isPlaying() == true) {
            dk1.stop();
            return;
        }
    
        if (dk2.isPlaying() == true) {
            dk2.stop();
        } else {
             dk1.start();
             dk1.setLooping(true);
        }
     }
    
     public void playdk2 (View view) {
        if (dk2.isPlaying() == true) {
            dk2.stop();
            return;
        }
    
        if (dk1.isPlaying() == true) {
            dk1.stop();
        } else {
              dk2.start();
              dk2.setLooping(true);
        }
    }
    

1 个答案:

答案 0 :(得分:0)

public void playdk1 (View view) 
if (dk1.isPlaying() == true) {
     dk1.stop();   
   }
 else{
    dk2.start();
    dk2.setLooping(true);}

if (dk2.isPlaying() == true) {
  dk2.stop();
} else {
dk1.start();
dk1.setLooping(true);
}
}

 public void playdk2 (View view) {
 if (dk2.isPlaying() == true) {
     dk2.stop();

     }
      else {
          dk1.start();
          dk1.setLooping(true);
        }

     if (dk1.isPlaying() == true) {
       dk1.stop();
       } else {
          dk2.start();
          dk2.setLooping(true);
        }
         }