与AudioRecord在Android上录制加快音频? [英] Recording with AudioRecord on Android speeds up the audio?

查看:361
本文介绍了与AudioRecord在Android上录制加快音频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AudioRecord记录原始音频进行处理。 音频记录完全没有任何噪声,但是当产生的原始PCM数据被重放时,它起着好像它已被加速很多(高达约两倍)。 我查看和播放PCM数据Audacity的。我使用实际的手机(三星Galaxy S5670)进行测试。 完成录音后在44100赫兹,16位。任何想法可能会导致这样?

I am using AudioRecord to record raw audio for processing. The audio records entirely without any noise but when the raw PCM data generated is played back, it plays as if it has been speeded up a lot (upto about twice as much). I am viewing and playing the PCM data in Audacity. I am using actual phone (Samsung Galaxy S5670) for testing. The recording is done at 44100 Hz, 16 bit. Any idea what might cause this?

以下是录音code:

public class TestApp extends Activity
{   
File file;
OutputStream os;
BufferedOutputStream bos;       
AudioRecord recorder;   
int iAudioBufferSize;
boolean bRecording; 
int iBytesRead;

Thread recordThread = new Thread(){
    @Override
    public void run()
    {
        byte[] buffer = new byte[iAudioBufferSize];
        int iBufferReadResult;
        iBytesRead = 0;
        while(!interrupted())
        {
            iBufferReadResult = recorder.read(buffer, 0, iAudioBufferSize);
            // Android is reading less number of bytes than requested.
            if(iAudioBufferSize > iBufferReadResult)
            {
                iBufferReadResult = iBufferReadResult + 
                        recorder.read(buffer, iBufferReadResult - 1, iAudioBufferSize - iBufferReadResult);
            }               
            iBytesRead = iBytesRead + iBufferReadResult;
            for (int i = 0; i < iBufferReadResult; i++)
            {
                try
                {
                    bos.write(buffer[i]);
                } catch (IOException e)
                {                                               
                    e.printStackTrace();
                }
            }
        }
    }
};  

@Override
public void onCreate(Bundle savedInstanceState)
{
    // File Creation and UI init stuff etc.

    bRecording = false;
    bPlaying = false;

    int iSampleRate = AudioTrack.getNativeOutputSampleRate(AudioManager.STREAM_SYSTEM);
    iAudioBufferSize = AudioRecord.getMinBufferSize(iSampleRate, AudioFormat.CHANNEL_IN_MONO, AudioFormat.ENCODING_PCM_16BIT); 
    recorder = new AudioRecord(AudioSource.MIC, iSampleRate, AudioFormat.CHANNEL_IN_MONO, 
        AudioFormat.ENCODING_PCM_16BIT, iAudioBufferSize);

    bt_Record.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v)
        {
            if (!bRecording)
            {
                try
                {                       
                    recorder.startRecording();
                    bRecording = true;
                    recordThread.start();                       
                }
                catch(Exception e)
                {
                    tv_Error.setText(e.getLocalizedMessage());
                }
            }
            else
            {
                recorder.stop();
                bRecording = false;
                recordThread.interrupt();
                try
                {
                    bos.close();
                }
                catch(IOException e)
                {

                }
                tv_Hello.setText("Recorded Sucessfully. Total " + iBytesRead + " bytes.");
            }
        }

    });
}
}

解决:我与它挣扎了1-2天之后公布这一点。不过,讽刺的是,我张贴后,找到了解决办法很快。缓冲输出流写入正在采取太多的时间在for循环中,所以流逃课样品。改成了块写入,删除for循环。完美。

RESOLVED : I posted this after struggling with it for 1-2 days. But, ironically, I found the solution soon after posting. The buffered output stream write was taking too much time in the for loop, so the stream was skipping samples. changed it to block write, removing the for loop. Works perfectly.

推荐答案

音频跳跃是由以书面形式向缓冲延迟造成的。 解决的方法是只需更换for循环:

The audio skipping was caused by the delay in writing to buffer. the solution is to just replace this FOR loop:

        for (int i = 0; i < iBufferReadResult; i++)
        {
            try
            {
                bos.write(buffer[i]);
            } catch (IOException e)
            {                                               
                e.printStackTrace();
            }
        }

由一个单一的写,像这样:

by a single write, like so:

       bos.write(buffer, 0, iBufferReadResult);

我用从书里面工作的code,我想,对于较低的采样率和缓冲的更新。

I had used the code from a book which worked, I guess, for lower sample rates and buffer updates.

这篇关于与AudioRecord在Android上录制加快音频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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