【问题标题】:android - how to delay an audio file by a few secondsandroid - 如何将音频文件延迟几秒钟
【发布时间】: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


【解决方案1】:

不要开始播放onCreate() 中的音​​频。从onCreate() 中删除mp.start()

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 并做一个简单的检查,看看您是否应该播放音频。

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

【讨论】:

  • 我是这样编辑的,但是现在播放器根本没有启动,代码在上面
  • 带有日志.. 检查是否正在调用 if 语句中的代码。
  • 另外,打印millisUntilFinished的值。
  • 不知道怎么做,你能通过编辑上面的代码告诉我吗?很抱歉,但我仍在学习在 android 中编写代码 :)
  • 我仍然遇到问题 - millisUntilFinished 不是字符串,它说我必须更改该变量的类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 2022-01-18
  • 2018-04-08
  • 2019-10-25
  • 1970-01-01
相关资源
最近更新 更多