2

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

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

欢迎任何和所有帮助。

4

2 回答 2

8

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;

...

于 2013-08-26T13:48:30.603 回答
0

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

于 2014-05-19T21:33:22.947 回答