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

Save recorded audio to file - OpenSL ES - Android(将录制的音频保存到文件 - OpenSL ES - Android)
本文介绍了将录制的音频保存到文件 - 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)