Save recorded audio to file - 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:将录制的音频保存到文件 - OpenSL ES - Android
基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01