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

标签 android audio android-ndk save

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

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

欢迎任何帮助。

最佳答案

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;

...

关于android - 将录制的音频保存到文件 - OpenSL ES - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18444354/

相关文章:

android - 来自 Firebase storageReference 的 getBytes 不起作用

java - AndroidViewModel 没有零参数构造函数

android - 在 Android/iOS 上模拟多人游戏技术

iphone - 有什么方法可以在屏幕锁定时在 iPhone 上播放音频,但在设置静音开关时不能播放?

python - 从numpy生成的wav文件无法听见(完全静音)

android - "local symbol ' __bss_start ' in global part of symbol table"仅在 Android NDK aarch64 构建中

java - 将 java 程序与 android 应用程序连接

delphi - Winmm.dll无法播放某些WAV声音吗?

java - Android NDK OpenGL ES 渲染图形到 Java 位图

ffmpeg - 使用 Android NDK 从 OGG 音乐文件中提取原始音频帧