简体   繁体   English

在 Android 中使用 MediaRecorder API 代替 Intent 进行相机录制

[英]Use MediaRecorder API instead of Intent for camera recording in Android

I'm using the following code to record the camera in Android that uses the basic Intent .我正在使用以下代码在使用基本Intent的 Android 中记录相机。 How can I change it to use MediaRecorder API instead of this Intent-based approach?如何将其更改为使用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 
    }

I don't use the Android SDK (my Java skills are on Windows only), but from a quick research...我不使用 Android SDK (我的 Java 技能仅适用于 ZAEA23489CE3AA9B6406EBB2,但仅来自快速研究 A...08)

See if this tutorial helps you (more info further beow)...看看本教程是否对您有帮助(更多信息见下文)...
Just replace their audio setup (3GP) with the H264 & AAC setup from the other Question .只需用另一个问题的 H264 和 AAC 设置替换他们的音频设置 (3GP)。
Also read the section about "Recording Video" on this article to understand the needed parts.另请阅读本文中有关“录制视频”的部分以了解所需的部分。

Also declare permissions in your Manifest.xml.还要在 Manifest.xml 中声明权限 An example of such manifest and related code is here on Github (that one allows RECORD_AUDIO but you need also a RECORD_VIDEO so set as):此类清单和相关代码的示例在 Github 上(允许RECORD_AUDIO但您还需要一个RECORD_VIDEO设置为):

 <uses-permission android:name="android.permission.RECORD_VIDEO"/>

Back to the tutorial, I cannot test their code but something like the below edit should get you started:回到教程,我无法测试他们的代码,但类似下面的编辑应该让你开始:

public class MainActivity extends Activity 
{  
    MediaRecorder recorder;  
    File outputfile = null;  
    static final String TAG = "MediaRecording"; 

Then inside that class you add useful functions (initialise Recorder, start rec, stop rec, save file)...然后在 class 里面添加有用的功能(初始化记录器,开始记录,停止记录,保存文件)......

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();
}

So to begin a recording, just inititalise the media recorder and do a recorder.start();因此,要开始录制,只需初始化媒体录制器并执行recorder.start(); to begin recording (camera and mic are used automatically according to your MediaRecorder settings).开始录制(根据您的 MediaRecorder 设置自动使用相机和麦克风)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM