这是我想要实现的目标:当用户按下“按钮 A”时,30 秒的“sound.mp3”开始反复播放 200 秒然后停止。“按钮 B”播放相同的声音 400 秒并停止。这是我最后一次尝试,使用 CountDownTimer (不确定这是否是最好的方法,甚至可能):
public class MyClass extends Activity {
MediaPlayer player_test;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_rain);
my_player();
}
public void my_player(){
player_test = MediaPlayer.create(getApplicationContext(), R.raw.mysound);
player_test.setLooping(true);
player_test.start();
}
public void onCompletion(MediaPlayer player_test) {
player_test.stop();
player_test.release();
}
});
public void player_OnClick(View v) {
switch (v.getId()) {
case R.id.button_a:
new CountDownTimer(200000, 1000) {
public void onTick(long millisUntilFinished) {
player_test.start();
}
public void onFinish() {
player_test.pause();
}
}.start();
break;
case R.id.button_b:
new CountDownTimer(400000, 1000) {
public void onTick(long millisUntilFinished) {
player_test.start();
}
public void onFinish() {
player_test.pause(); }
}.start();
break;
}
}
谢谢您的回答。