【发布时间】:2015-03-27 07:04:24
【问题描述】:
我试图弄清楚如何将音频文件延迟大约 15 秒。音频文件有 5 秒长,它是一个声音效果,说“5,4,3,2,1 go!”,倒计时是从 20 开始的,所以它应该在 15 秒后开始。我听说我可以用处理程序来做到这一点,但我不知道如何在代码中实现它,所以在这里,非常感谢帮助!
我用下面的方式编辑了它,但是现在播放器根本没有启动,这是新代码:
public class WorkoutGymEasy1 extends Activity {
CountDownTimer cdt = null;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.workout_gym_easy1);
RelativeLayout rl5 = (RelativeLayout) findViewById(R.id.rl5);
rl5.setBackgroundColor(Color.BLACK);
mp = MediaPlayer.create(WorkoutGymEasy1.this, R.raw.countdown);
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
new CountDownTimer(20000, 1000) { //20 seconds count down with 1s interval (1000 ms)
TextView c = (TextView) findViewById(R.id.timer_gym_easy1); //access TextView element on the screen to set the timer value
public void onTick(long millisUntilFinished) { // Code in this method is executed every 1000ms (1s)
c.setText("" + ((millisUntilFinished / 1000) - 1) + ""); //update the timer
if(millisUntilFinished == 9000) {
mp.start();
}
}
@Override
public void onFinish() {
// TODO Auto-generated method stub
c.setText("GO!");
Intent myIntent = new Intent(getApplicationContext(), WorkoutGymEasy2.class);
startActivity(myIntent);
finish(); // call finish() method to destroy this activity and return to instructions screen
}
}.start();
}
protected void OnPause() {
super.onPause();
mp.release();
finish();
}
}
【问题讨论】:
-
使用给定的音频文件初始化您的播放器,启动一个 15 秒的可运行文件,同时调用处理程序并开始在其中播放播放器。
标签: android android-activity android-mediaplayer android-handler