潜伏在Android应用程序播放一个.wav / .MP3 / .OGG音频文件 [英] Latency in playing a .wav/.mp3/.ogg audio file in Android App

查看:170
本文介绍了潜伏在Android应用程序播放一个.wav / .MP3 / .OGG音频文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

但由于延迟,它起着次数在于978和984之间我也试过其他音频格式(.OGG,.MP3等)。

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

我分享我的code以下:

 包com.abhinav.soundlooper;进口android.media.MediaPlayer;
进口android.os.Bundle;
进口android.os.CountDownTimer;
进口android.app.Activity;
进口android.util.Log;
进口android.view.KeyEvent;
进口android.view.Menu;
进口android.view.View;
进口android.view.View.OnClickListener;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.TextView;公共类MainActivity扩展活动实现OnClickListener {按钮btnStart;
TextView的tvPause,tvLoop;
的EditText etPause,etLoop;
长时间的停顿,循环;
私人CountDownTimer定时器;
MediaPlayer的熔点;
公共线程t;@覆盖
保护无效的onCreate(捆绑savedInstanceState){    super.onCreate(savedInstanceState);    的setContentView(R.layout.activity_main);
    btnStart =(按钮)findViewById(R.id.btnStart);
// tvLoop =(的TextView)findViewById(R.id.etLoop)
    etPause =(EditText上)findViewById(R.id.etPause);
    etLoop =(EditText上)findViewById(R.id.etLoop);
    btnStart.setOnClickListener(本);
    熔点= MediaPlayer.create(getApplicationContext(),R.raw.beepwav);}@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
    //充气菜单;如果是present这增加了项目操作栏。
    。getMenuInflater()膨胀(R.menu.main,菜单);
    返回true;
}@覆盖
公共布尔的onkeydown(INT键code,KeyEvent的事件){    如果(键code == KeyEvent.KEY code_BACK&放大器;&安培; event.getRepeatCount()== 0){        完();
        返回true;
    }
    返回super.onKeyDown(键code,事件);
}@覆盖
公共无效的onClick(最终查看V){
    // TODO自动生成方法存根
//暂停= Integer.valueOf(etPause.getText()的toString())+ 1;
//循环= 1 + Integer.valueOf(etLoop.getText()的toString());
//笼屉=(暂停+ 30);
//长TT = TI *循环;        定时器=新CountDownTimer(30000,30){            INT I = 0;
             公共无效onTick(长millisUntilFinished){// v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
                    mp.start();
//尝试{
// timer.wait(10);
//}赶上(InterruptedException的E){
// // TODO自动生成catch块
// e.printStackTrace();
//}
                    我++;             }             公共无效onFinish(){
// mTextField.setText(完成了!);
                 Log.i(循环,+ I);
                 STOPALL();
// mp.stop();
// mp.release();             }
          }。开始();
//
}保护无效STOPALL(){
    // TODO自动生成方法存根
    timer.cancel();
}}


解决方案

我不知道,但如果1000毫秒为1秒。因此,30000 milliesconds为30秒。
如果音频是20毫秒,然后 30,000 / 20 1500 。所以音频是能够在30秒内被播放1.5K

 定时器=新CountDownTimer(30000,27){            INT I = 0;
             公共无效onTick(长millisUntilFinished){// v.playSoundEffect(android.view.SoundEffectConstants.CLICK);
                    mp.start();
//尝试{
// timer.wait(10);
//}赶上(InterruptedException的E){
// // TODO自动生成catch块
// e.printStackTrace();
//}
                    我++;             }

如果用30延迟它的950倍,如果我们降低延迟约27会怎么样?

,你可以随时你的if语句..

 如果(我== 1000){
             STOPALL();
}其他{
我++
}

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

}

解决方案

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

这篇关于潜伏在Android应用程序播放一个.wav / .MP3 / .OGG音频文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆