问题描述
我试图弄清楚如何将音频文件延迟约15秒.音频文件的时长为5秒,声音效果为"5,4,3,2,1 go!",倒数是从20开始的,因此应在15秒后开始播放.我听说我可以使用处理程序来执行此操作,但是我不知道如何在代码中实现它,所以在这里,非常感谢您的帮助!
I'm trying to figure out how to delay an audio file by about 15 seconds. The audio file is 5 seconds long and it's a sound effect saying "5,4,3,2,1 go!", the countdown is from 20 so it should start after 15 seconds. I heard I can do this with a handler but I don't know how to implement it in the code, so here it is, help would be much appreciated!
我按照以下方式进行了编辑,但是现在播放器根本无法启动,这是新代码:
I edited it with the way below, however now the player doesn't start at all, here's the new code:
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();
}
}
推荐答案
不要开始在onCreate()
中播放音频.从onCreate()
中删除mp.start()
.
Don't start playing the audio in onCreate()
. Remove mp.start()
from onCreate()
.
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();
}
在onTick()
中播放音频. CountDownTimer
并不精确,它会尽可能快地返回接近1秒的时间.永远不会是20000、19000等.作为优化,您可以在每个onTick()
上将millisUntilFinished
取整,并进行简单的检查以查看是否应该播放音频.
Play the audio in onTick()
. The CountDownTimer
is not precise, it will return as close to 1 second as it can. It will never be 20000, 19000... etc. As an optimization you can round the millisUntilFinished
on each onTick()
and do a simple check to see if you should play the audio or not.
public void onTick(long millisUntilFinished) { // Code in this method is executed every 1000ms (1s)
c.setText("" + ((millisUntilFinished / 1000) - 1) + ""); //update the timer
if(Math.round((float)millisUntilFinished / 1000.0f) == 5) {
mp.start();
}
}
这篇关于android-如何将音频文件延迟几秒钟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!