将录制的音频保存到文件 - OpenSL ES - Android [英] Save recorded audio to file - OpenSL ES - Android

查看:33
本文介绍了将录制的音频保存到文件 - OpenSL ES - Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to record from the microphone, add some effects, and the save this to a file

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

I've started with the example native-audio included in the Android NDK. I'va managed to add some reverb and play it back but I haven't found any examples or help on how to accomplish this.

欢迎任何和所有帮助.

推荐答案

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

OpenSL is not a framework for file formats and access. If you want a raw PCM file, simply open it for writing and put all buffers from OpenSL callback into the file. But if you want encoded audio, you need your own codec and format handler. You can use ffmpeg libraries, or built-in stagefright.

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

Update write playback buffers to local raw PCM file

我们从 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;

...

这篇关于将录制的音频保存到文件 - OpenSL ES - Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆