c_cpp 在Android NDK中使用MediaRecorder

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 在Android NDK中使用MediaRecorder相关的知识,希望对你有一定的参考价值。

#include "media/mediarecorder.h"

using namespace android;

MediaRecorder *mMediaRecorder = new MediaRecorder();

// Step 1: Unlock and set camera to MediaRecorder
camera_->unlock();
android::status_t err = mMediaRecorder->setCamera(camera_->remote(),
		camera_->getRecordingProxy());
LOGD("Error %d", err);

// Step 2: Set sources
err = mMediaRecorder->setVideoSource(VIDEO_SOURCE_CAMERA);
LOGD("Error %d", err);

// Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
err = mMediaRecorder->setOutputFormat(OUTPUT_FORMAT_MPEG_4);
LOGD("Error %d", err);
err = mMediaRecorder->setVideoEncoder(VIDEO_ENCODER_H264);
LOGD("Error %d", err);

// Step 4: Set output file
const char *fileName = "/sdcard/outputSurfEncMSourceGLDiff.mp4";
int fd = open(fileName, O_RDWR | O_CREAT, 0744);
if (fd < 0) {
	LOGD("ERROR: Could not open the the file %s, fd = %d !!", fileName,
			fd);
}
err = mMediaRecorder->setOutputFile(fd, 0, 0);

// Step 5: Set the preview output
// PS: I try to do without this step on android 4.1.1 but no success :(
mMediaRecorder.setPreviewDisplay(mPreview.getHolder().getSurface());

LOGD("Error %d", err);
err = mMediaRecorder->setVideoSize(640, 480);
LOGD("Error %d", err);
err = mMediaRecorder->setVideoFrameRate(30);
LOGD("Error %d", err);

// Step 6: Prepare configured MediaRecorder
err =  mMediaRecorder->prepare();
LOGD("Prepare err %d", err);

// Step 7: Start recording
err = mMediaRecorder->start();

// This is how to stop MediaRecorder from document: http://developer.android.com/guide/topics/media/camera.html
// Stop recording using MediaRecorder.stop()
// Release the media recorder with MediaRecorder.release()
// Lock the camera using Camera.lock()

以上是关于c_cpp 在Android NDK中使用MediaRecorder的主要内容,如果未能解决你的问题,请参考以下文章

NDK笔记-在Android Studio中使用ndk-build(转)

Android 逆向Android 进程注入工具开发 ( Visual Studio 开发 Android NDK 应用 | 使用 Makefile 构建 Android 平台 NDK 应用 )(代码

如何在 NDK 原生代码中获取断点并在 Android Studio 中调试原生代码?

错误:在Android ndk中使用C的冲突类型

Android底层基础之NDK

在 Android NDK 中使用 Skia