音频播放声音Android中的按钮问题

时间:2014-03-18 16:59:44

标签: android audio

我有4个按钮,2个是下一个和上一个,另外2个是imgBut1和imgBut2,通过使用2个数组我想通过点击这些图像按钮产生2个不同的声音但是当我点击下一个和前一个按钮时我的声音正在播放,我希望我的声音只有在我点击imgBut1和imgBut2时播放。 我的java代码:

 public class DoChashmiPics extends Activity {
         ImageButton imgBut1;
         ImageButton imgBut2;
         MediaPlayer ourSong1;
         MediaPlayer ourSong2;
         private int currentAudio = 0;

int[] audios1 = {R.raw.dochashm0,R.raw.dochashm1, R.raw.dochashm2, R.raw.dochashm3, R.raw.dochashm4, R.raw.dochashm5, R.raw.dochashm6, R.raw.dochashm7, R.raw.dochashm8, R.raw.dochashm9, R.raw.dochashm10, R.raw.dochashm11 };
int[] audios2 = {R.raw.dochashmi0,R.raw.dochashmi1, R.raw.dochashmi2, R.raw.dochashmi3, R.raw.dochashmi4, R.raw.dochashmi5, R.raw.dochashmi6, R.raw.dochashmi7, R.raw.dochashmi8, R.raw.dochashmi9, R.raw.dochashmi10, R.raw.dochashmi11 };


// my image button 1
public void imageButton1(View v) {
    // TODO Auto-generated method stub
}
  // my image button 2
public void imageButton2(View v) {
    // TODO Auto-generated method stub             
}

// my next button
    public void onClick(View v) {
        try {
  currentAudio = (++currentAudio + audios1.length) % audios1.length;
  currentAudio = (++currentAudio + audios2.length) % audios2.length;
ourSong1 = MediaPlayer.create(DoChashmiPics.this, audios1[currentAudio]);
            ourSong1.start();
ourSong2 = MediaPlayer.create(DoChashmiPics.this, audios2[currentAudio]);
            ourSong2.start();
        } catch (Exception e) {

        }

    }

};
    // my previous button
    public void onClick(View v) {
        try {           
    currentAudio = (--currentAudio + audios1.length) % audios1.length;
    currentAudio = (--currentAudio + audios2.length) % audios2.length;
ourSong1 = MediaPlayer.create(DoChashmiPics.this, audios1[currentAudio]);
            ourSong1.start();
ourSong2 = MediaPlayer.create(DoChashmiPics.this, audios2[currentAudio]);
            ourSong2.start();

        } catch (Exception e) {

        }

    }

};
}

1 个答案:

答案 0 :(得分:0)

移动媒体播放器图片按钮事件监听器中播放代码,然后就可以了

// my image button 1
public void imageButton1(View v) {
    // TODO Auto-generated method stub
ourSong1 = MediaPlayer.create(DoChashmiPics.this, audios1[currentAudio]);
            ourSong1.start();
ourSong2 = MediaPlayer.create(DoChashmiPics.this, audios2[currentAudio]);
            ourSong2.start();
        } catch (Exception e) {

        }

    }
}



// my image button 2
public void imageButton2(View v) {
    // TODO Auto-generated method stub 
ourSong1 = MediaPlayer.create(DoChashmiPics.this, audios1[currentAudio]);
            ourSong1.start();
ourSong2 = MediaPlayer.create(DoChashmiPics.this, audios2[currentAudio]);
            ourSong2.start();

        } catch (Exception e) {

        }

    }            
}

// my next button
    public void onClick(View v) {
        try {
  currentAudio = (++currentAudio + audios1.length) % audios1.length;
  currentAudio = (++currentAudio + audios2.length) % audios2.length;

};
    // my previous button
    public void onClick(View v) {
        try {           
    currentAudio = (--currentAudio + audios1.length) % audios1.length;
    currentAudio = (--currentAudio + audios2.length) % audios2.length;

};