Latency in playing a .wav/.mp3/.ogg audio file in Android App

别来无恙 提交于 2019-12-22 23:23:50

问题


I'd written an application for playing 20 millisecond audio clip(.wav format). It simply plays the sound clip repeatedly 1000 times.

But due to Latency, number of times it plays lies between 978 and 984. I'd also tried other audio format(.ogg, .mp3, etc).

I want reduce the latency and also to get reliable number.

I'm sharing my code below:

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

}

回答1:


I'm not sure but if 1000 milliseconds is 1 second. So 30,000 milliesconds is 30 seconds. and if the audio is 20 milliseconds then 30,000/20 is 1,500. So the audio is capable of being played 1.5K in 30 seconds.

        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++;

             }

if with the 30 latency it's 950 times, what happens if we lower the latency to about 27?

and you can always you if statement ..

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


来源:https://stackoverflow.com/questions/18463765/latency-in-playing-a-wav-mp3-ogg-audio-file-in-android-app

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