【问题标题】:Android AudioRecord delay when using bytes, not when using and shorts使用字节时的Android AudioRecord延迟,而不是使用和短路时
【发布时间】:2018-03-02 03:04:40
【问题描述】:

我正在开发一个 Xamarin Android 应用程序,在该应用程序中录制视频和音频以及一些正在播放的音乐。我需要将所有这些流合并在一起。

我试图弄清楚在 audioRecord.read() 函数中使用 byte[](或我也尝试过的 ByteBuffer)录制音频时缺少什么。输出的 WAV 文件似乎是正确的(显然可以在 44100Hz 采样率下播放),但几秒钟后会出现延迟,并且会变得越来越大。

使用短裤时,我在 MIC 录制的音频中没有任何延迟。使用短裤的最大问题是,不管我做什么,我不能有高于 8000hz 的采样率(但这不是当前的问题,尽管如果有人知道如何解决它,我会接受它:))

最终合并的文件是带有 AAC 音频的 mp4,使用 ffmpeg 合并,但我认为这不是问题。

会不会与 8000Hz(使用短)和 44100Hz(使用字节)有关?或者我在使用 byte[] 时添加了一些东西,因为我不检查读取了多少字节?

以下是问题涉及的部分:

//output file initialization
mDataOutputStream = new FileOutputStream(new Java.IO.File(mRawFilePath));

public void Run()
{

...

short[] shortBuf = new short[bufferSize / 2];
//byte[] byteBuf = new byte[bufferSize];

while(isRecording) {
    //using shorts
    audioRecorder.Read(shortBuf, 0, shortBuf.Length);
    WriteShortsToFile(buf);

    //using byte[]
    //audioRecorder.Read(byteBuf, 0, byteBuf.Length);
    //WriteBytesToFile(buf);    
}

...

}

public void WriteShortsToFile(short[] shorts)
{
    for (int i = 0; i < shorts.Length; i++)
    {
        mDataOutputStream.WriteByte(shorts[i] & 0xFF);
        mDataOutputStream.WriteByte((shorts[i] >> 8) & 0xFF);
    }
}

public void WriteBytesToFile(byte[] buf)
{
    mDataOutputStream.Write(buf);
}

【问题讨论】:

  • 它应该是 WriteShortsToFile(shortBuf) - 在我的代码中它是正确的,因为我为这个问题调整了变量名

标签: android xamarin audiorecord


【解决方案1】:

终于让它正常工作了。

我将短数组的分配大小更改为bufferSize。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2011-07-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-15
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    相关资源
    最近更新 更多