【问题标题】:Save recorded audio to file - OpenSL ES - Android将录制的音频保存到文件 - OpenSL ES - Android
【发布时间】:2013-08-26 12:41:24
【问题描述】:

我正在尝试从麦克风录制,添加一些效果,然后将其保存到文件中

我从 Android NDK 中包含的示例原生音频开始。 我设法添加了一些混响并播放它,但我没有找到任何示例或有关如何完成此操作的帮助。

欢迎任何和所有帮助。

【问题讨论】:

  • “我设法添加了一些混响并播放” 所以你已经让它工作了..?你能澄清一下实际的问题是什么吗?
  • 问题是将音频保存到文件中,我不知道该怎么做

标签: android audio android-ndk save


【解决方案1】:

OpenSL 不是文件格式和访问的框架。如果您想要原始 PCM 文件,只需打开它进行写入并将 OpenSL 回调中的所有缓冲区放入文件中。但是,如果您想要编码音频,则需要您自己的编解码器和格式处理程序。你可以使用 ffmpeg 库,或者内置的 stagefright。

更新将播放缓冲区写入本地原始 PCM 文件

我们从 native-audio-jni.c 开始

#include <stdio.h>
FILE* rawFile = NULL;
int bClosing = 0;

...

void bqPlayerCallback(SLAndroidSimpleBufferQueueItf bq, void *context)
{
    assert(bq == bqPlayerBufferQueue);
    assert(NULL == context);
    // for streaming playback, replace this test by logic to find and fill the next buffer
    if (--nextCount > 0 && NULL != nextBuffer && 0 != nextSize) {
        SLresult result;
        // enqueue another buffer
        result = (*bqPlayerBufferQueue)->Enqueue(bqPlayerBufferQueue, nextBuffer, nextSize);
        // the most likely other result is SL_RESULT_BUFFER_INSUFFICIENT,
        // which for this code example would indicate a programming error
        assert(SL_RESULT_SUCCESS == result);
        (void)result;

        // AlexC: here we write:
        if (rawFile) {
            fwrite(nextBuffer, nextSize, 1, rawFile);
        }
    }
    if (bClosing) { // it is important to do this in a callback, to be on the correct thread
        fclose(rawFile);
        rawFile = NULL;
    }
    // AlexC: end of changes
}

...

void Java_com_example_nativeaudio_NativeAudio_startRecording(JNIEnv* env, jclass clazz)
{
    bClosing = 0;
    rawFile = fopen("/sdcard/rawFile.pcm", "wb");

...

void Java_com_example_nativeaudio_NativeAudio_shutdown(JNIEnv* env, jclass clazz)
{
    bClosing = 1;

...

【讨论】:

  • 能否看一下您提出的解决方案的示例?
  • 请第一个,将缓冲区保存到文件中。
  • 当然,只要我使用 PC。
  • 谢谢,这将是一个救命稻草,到目前为止,其中一个拔掉你的头发,吃掉你自己的手臂对我来说有点问题。
  • 亚历克斯。我做了一些谷歌搜索并弄清楚了,这里的关键是我需要在读取数据的同时进行记录。无论如何,您的回答确实提供了一个很好的起点,谢谢。
【解决方案2】:

将原始向量从c传递到java并使用mediaRecorder将其编码为mp3,我不知道您是否可以从原始向量中设置音频源,但也许......

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 2012-07-04
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    相关资源
    最近更新 更多