在 Android 中使用 MediaRecorder API 而不是 Intent 进行摄像头录制
Posted
技术标签:
【中文标题】在 Android 中使用 MediaRecorder API 而不是 Intent 进行摄像头录制【英文标题】:Use MediaRecorder API instead of Intent for camera recording in Android 【发布时间】:2022-01-17 19:19:29 【问题描述】:我正在使用以下代码在使用基本Intent
的 android 中记录相机。如何将其更改为使用 MediaRecorder
API 而不是这种基于 Intent 的方法?
private Uri fileUri;
//...
private void recordVideo()
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
// set video quality
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
// start the video capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
// ...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == RESULT_OK)
// play the video given the global fileUri
【问题讨论】:
【参考方案1】:我不使用 Android SDK(我的 Java 技能仅适用于 Windows),但通过快速研究...
看看this tutorial 是否对您有帮助(更多信息请见下文)... 只需使用来自other Question 的 H264 和 AAC 设置替换他们的音频设置 (3GP)。 另请阅读this article 上关于“录制视频”的部分以了解所需部分。
还有 declare permissions 在您的 Manifest.xml 中。这种清单和相关代码的一个示例是here on Github(允许RECORD_AUDIO
,但您还需要一个RECORD_VIDEO
,因此设置为):
<uses-permission android:name="android.permission.RECORD_VIDEO"/>
回到教程,我无法测试他们的代码,但下面的编辑应该可以帮助你开始:
public class MainActivity extends Activity
MediaRecorder recorder;
File outputfile = null;
static final String TAG = "MediaRecording";
然后在该类中添加有用的功能(初始化记录器、启动记录、停止记录、保存文件)...
public void initRecording(View view) throws IOException
//Creating file
File dir = Environment.getExternalStorageDirectory();
try outputfile = File.createTempFile("video_test_android", ".mp4", dir);
catch (IOException e)
Log.e(TAG, "external storage access error");
return;
//# Create a new instance of MediaRecorder
recorder = new MediaRecorder(); //create MediaRecorder object
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setAudiosource(MediaRecorder.AudioSource.CAMCORDER);
recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
//# Video settings
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //contained inside MP4
recorder.setVideoSize(640, 480); //width 640, height 480
recorder.setVideoFrameRate(30); //30 FPS
recorder.setVideoEncodingBitRate(3000000); //adjust this for picture quality
//# Audio settings
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); //must always be AAC
recorder.setAudioEncoder(MediaRecorder.getAudioSourceMax());
recorder.setAudioEncodingBitRate(16);
recorder.setAudioSamplingRate(44100);
recorder.setOutputFile(outputfile.getAbsolutePath());
recorder.prepare();
public void startRecording(View view)
recorder.start();
public void stopRecording(View view)
recorder.stop(); recorder.release();
//# After stopping the recorder...
//# Create the video file and add it to the Media Library.
addRecordingToMediaLibrary();
protected void addRecordingToMediaLibrary()
//# used to store a set of values that the ContentResolver can process
ContentValues values = new ContentValues(4); //# 4 items (title, date, etc)
long current = System.currentTimeMillis(); //# get recording time (date)
//# size of 4 for values, all in String
values.put(MediaStore.Audio.Media.TITLE, "Android Tina J - " + outputfile.getName());
values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
values.put(MediaStore.Audio.Media.MIME_TYPE, "video/mp4"); //# set MIME type
values.put(MediaStore.Audio.Media.DATA, outputfile.getAbsolutePath()); // data stream for the file
//# provides applications access to the content model
ContentResolver contentResolver = getContentResolver();
//# The content:// style URI for the "primary" external storage volume.
Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri newUri = contentResolver.insert(base, values);
//# Request the media scanner to scan a file and add it to the media database
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
//# if you need a notification...
//Toast.makeText(this, "Created Media File : " + newUri, Toast.LENGTH_LONG).show();
所以要开始录制,只需初始化媒体录制器并发送recorder.start();
即可开始录制(相机和麦克风会根据您的 MediaRecorder 设置自动使用)。
【讨论】:
谢谢。我做了大部分这些步骤......但运行它仍然有问题。以上是关于在 Android 中使用 MediaRecorder API 而不是 Intent 进行摄像头录制的主要内容,如果未能解决你的问题,请参考以下文章