Android多媒体应用开发系列(四) 使用MediaRecorder录像

来源:互联网 发布:一叶落而知秋,下一句 编辑:程序博客网 时间:2024/06/07 03:33

辛苦堆砌,转载请注明出处,谢谢!


          上一篇文章完成了录音的功能,这篇再来看看录像,依然是使用MediaRedorder,所以不再帖状态图,不了解的可以看一下多媒体系列第三篇开篇,另外,由于录像需要使用Camera,因此,不妨在第二篇中的MyCamera的基础上开发,这样,我们的摄像头功能就更强大了。

        首先看一下界面部分,由于要使用Camera,所以需要一个SurfaceView,第一篇文章中已经详细说明了,这里不再赘述。

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.yjp.videorecorder.MainActivity">    <SurfaceView        android:id="@+id/surfaceView"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <Button        android:id="@+id/videoRecordingButton"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentBottom="true"        android:layout_centerHorizontal="true"        android:layout_marginBottom="20dp"        android:text="@string/startVideoRecording"/></RelativeLayout>

项目中加入之前的MyCamera类,然后添加权限

<!--suppress DeprecatedClassUsageInspection --><uses-feature android:name="android.hardware.Camera" /><uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.RECORD_AUDIO" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
上面已经加入了录音的权限,录制视频包含录音,需要添加该权限

此时,MainActivity类如下:

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback {    private MyCamera mCamera = new MyCamera();    @SuppressWarnings("deprecation")    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //配置SurfaceView        //setType使用外来数据源        //设置SurfaceHolder.Callback        SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView);        SurfaceHolder surfaceHolder = surfaceView.getHolder();        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        surfaceHolder.addCallback(this);        Button videoRecordingButton = (Button) findViewById(R.id.videoRecordingButton);        videoRecordingButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Button button = (Button) v;                if (button.getText().equals(getResources().getString(R.string.startVideoRecording))) {                    button.setText(getResources().getString(R.string.stopVideoRecording));                } else {                    button.setText(getResources().getString(R.string.startVideoRecording));                }            }        });    }    @Override    protected void onStart() {        super.onStart();        mCamera.openCamera();    }    @Override    protected void onStop() {        mCamera.releaseCamera();        super.onStop();    }    @Override    public void surfaceCreated(SurfaceHolder holder) {        mCamera.onSurfaceCreated(holder);    }    @Override    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {        mCamera.onSurfaceChanged(holder, format, width, height);    }    @Override    public void surfaceDestroyed(SurfaceHolder holder) {        mCamera.onSurfaceDestroyed(holder);    }}

主要是按钮状态和配置SurfaceView,下面才是重头戏,在MyCamera中添加录像功能,主要涉及几个函数

public class MyCamera implements Camera.PictureCallback, Camera.ShutterCallback {    ...    private MediaRecorder mVideoRecorder;    private Surface mVideoRecorderSurface;    ...    public void prepareVideoRecorder(String filePathname) {        if (mVideoRecorder != null) {            resetVideoRecorder();            releaseVideoRecorder();        }        mVideoRecorder = new MediaRecorder();        try {            //解锁摄像头,方便MediaRecorder使用            mCamera.unlock();            mVideoRecorder.setCamera(mCamera);            //设置数据源            mVideoRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);            mVideoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);            //视频质量            CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);            mVideoRecorder.setProfile(cpHigh);            //设置输出文件            mVideoRecorder.setOutputFile(filePathname);            //设置预览使用的surface            mVideoRecorder.setPreviewDisplay(mVideoRecorderSurface);            //准备            mVideoRecorder.prepare();        } catch (Exception e) {            e.printStackTrace();            releaseVideoRecorder();        }    }    public void releaseVideoRecorder() {        if (mVideoRecorder != null) {            resetVideoRecorder();            mVideoRecorder.release();            mVideoRecorder = null;        }    }    private void resetVideoRecorder() {        if (mVideoRecorder != null) {            mVideoRecorder.reset();            mCamera.lock();        }    }    public void startVideoRecording() {        try {            mVideoRecorder.start();        } catch (IllegalStateException e) {            e.printStackTrace();            resetVideoRecorder();        }    }    public void stopVideoRecording() {        try {            mVideoRecorder.stop();        } catch (IllegalStateException e) {            e.printStackTrace();            resetVideoRecorder();        }    }    ...}

是不是很简单,关键的步骤主要在prepare之前的配置,注释已经有说明,最后,修改MainActivity中的按钮监听器,添加对上面接口的调用

videoRecordingButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Button button = (Button) v;if (button.getText().equals(getResources().getString(R.string.startVideoRecording))) {button.setText(getResources().getString(R.string.stopVideoRecording));String videoDir = Environment.getExternalStorageDirectory() + "/" + "video/";File dir = new File(videoDir);if (!dir.exists()) {dir.mkdirs();}String videoFilePathname = videoDir + new Date().getTime() + ".mp4";mCamera.prepareVideoRecorder(videoFilePathname);mCamera.startVideoRecording();} else {button.setText(getResources().getString(R.string.startVideoRecording));mCamera.stopVideoRecording();}}});

运行体验一下吧!

 源码在这里





1 0