繁体   English   中英

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

[英]Save recorded audio to file - OpenSL ES - Android

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

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

任何帮助都欢迎。

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

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

#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;

...

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM