【问题标题】:Latency in playing a .wav/.mp3/.ogg audio file in Android App在 Android 应用程序中播放 .wav/.mp3/.ogg 音频文件的延迟
【发布时间】:2013-08-27 11:10:12
【问题描述】:

我编写了一个播放 20 毫秒音频剪辑(.wav 格式)的应用程序。 它只是重复播放声音片段 1000 次。

但由于延迟,它播放的次数介于 978 和 984 之间。我还尝试了其他音频格式(.ogg、.mp3 等)。

我想减少延迟并获得可靠的数字。

我在下面分享我的代码:

package com.abhinav.soundlooper;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.app.Activity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

Button btnStart;
TextView tvPause, tvLoop;
EditText etPause, etLoop;
long pause, loop;
private CountDownTimer timer;
MediaPlayer mp;
public Thread t;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    btnStart = (Button) findViewById(R.id.btnStart);
//      tvLoop = (TextView) findViewById(R.id.etLoop)
    etPause = (EditText) findViewById(R.id.etPause);
    etLoop = (EditText) findViewById(R.id.etLoop);
    btnStart.setOnClickListener(this);
    mp = MediaPlayer.create(getApplicationContext(), R.raw.beepwav);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {

    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

@Override
public void onClick(final View v) {
    // TODO Auto-generated method stub
//      pause = Integer.valueOf(etPause.getText().toString()) + 1 ;
//      loop = 1 + Integer.valueOf(etLoop.getText().toString());
//      long ti = (pause+30);
//      long tt = ti*loop;

        timer = new CountDownTimer(30000, 30) {

            int i =0;
             public void onTick(long millisUntilFinished) {

//                   v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
                    mp.start();
//                      try {
//                          timer.wait(10);
//                      } catch (InterruptedException e) {
//                          // TODO Auto-generated catch block
//                          e.printStackTrace();
//                      }
                    i++;

             }

             public void onFinish() {
//                   mTextField.setText("done!");
                 Log.i("loop", ""+i);
                 stopAll();
//                   mp.stop();
//                   mp.release();

             }
          }.start();
//            
}

protected void stopAll() {
    // TODO Auto-generated method stub
    timer.cancel();
}

}

【问题讨论】:

  • 问题是什么?
  • 迈克尔我已经编辑了我的问题。

标签: android audio android-mediaplayer


【解决方案1】:

我不确定,但如果 1000 毫秒是 1 秒。所以 30,000 毫秒是 30 秒。 如果音频为 20 毫秒,则 30,000/201,500。所以音频可以在 30 秒内播放 1.5K。

        timer = new CountDownTimer(30000, 27) {

            int i =0;
             public void onTick(long millisUntilFinished) {

//                   v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
                    mp.start();
//                      try {
//                          timer.wait(10);
//                      } catch (InterruptedException e) {
//                          // TODO Auto-generated catch block
//                          e.printStackTrace();
//                      }
                    i++;

             }

如果 30 的延迟是 950 次,如果我们将延迟降低到大约 27 会发生什么?

而且你总是可以使用 if 语句 ..

if (i == 1000){
             stopAll();
} else {
i++
}

【讨论】:

  • 我想要一个可靠的计数,因为我必须使用这个应用程序来测试另一个应用程序
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-06-19
  • 1970-01-01
  • 2012-02-10
  • 1970-01-01
  • 2013-04-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多