将录制的音频保存到文件 - OpenSL ES - Android

Mik*_*ant 2 audio android save android-ndk

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

我已经开始使用Android NDK中包含的示例原生音频.我设法添加一些混响并播放它,但我没有找到任何例子或帮助如何实现这一点.

欢迎任何和所有帮助.

Ale*_*ohn 7

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

将写回放缓冲区更新为本地原始PCM文件

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

#include <stdio.h>
FILE* rawFile = NULL;
int bClosing = 0;
Run Code Online (Sandbox Code Playgroud)

...

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
}
Run Code Online (Sandbox Code Playgroud)

...

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

...

void Java_com_example_nativeaudio_NativeAudio_shutdown(JNIEnv* env, jclass clazz)
{
    bClosing = 1;
Run Code Online (Sandbox Code Playgroud)

...