android - how to delay an audio file by a few seconds

强颜欢笑 提交于 2020-02-07 08:57:25

问题


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();
}
}

回答1:


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();
}

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();
  }
}


来源:https://stackoverflow.com/questions/28191266/android-how-to-delay-an-audio-file-by-a-few-seconds

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!