Android使用MediaRecorder类实现视频和音频录制功能 |
您所在的位置:网站首页 › 安卓录制的视频是什么格式的 › Android使用MediaRecorder类实现视频和音频录制功能 |
![]() 一、前期基础知识储备 Android提供了MediaRecorder这一个类来实现视频和音频的录制。 ![]() 由官方配图可知,MediaRecorder用于录制视频时需要调用一系列的API来设置和录制相关的配置,而且调用方法的顺序是固定的,必须按照这个顺序进行API调用才能正确利用手机摄像头实现录像功能。 调用MediaRecorder的录像API顺序如下: 1)Open Camera – Use the Camera.open() to get an instance of the camera object. 2)Connect Preview – Prepare a live camera image preview by connecting a SurfaceView to the camera using Camera.setPreviewDisplay(). 3)Start Preview – Call Camera.startPreview() to begin displaying the live camera images. 4)Start Recording Video – The following steps must be completed in order to successfully record video: a.Unlock the Camera – Unlock the camera for use by MediaRecorder by calling Camera.unlock(). b.Configure MediaRecorder – Call in the following MediaRecorder methods in this order: setCamera() – Set the camera to be used for video capture,绑定Camera进行视频录制。 setAudioSource() – Set the audio source,设置音频源。 setVideoSource() – Set the video source,设置视频源。 setProfile() – Set the video output format and encoding,录制效果的配置。 setOutputFile() – Set the output file, 设置录制好的文件存储位置。 setPreviewDisplay() – Connect Preview,设置预览效果。 c.Prepare MediaRecorder- Prepare the MediaRecorder with provided configuration settings by calling MediaRecorder.prepare(). d.Start MediaRecorder – Start recording video by calling MediaRecorder.start(). 停止录像时调用的API顺序如下: 1)Stop MediaRecorder – Stop recording video by calling MediaRecorder.stop(). 2)Reset MediaRecorder – Optionally, remove the configuration settings from the recorder by calling MediaRecorder.reset(). 3)Release MediaRecorder – Release the MediaRecorder by calling MediaRecorder.release(). 4)Lock the Camera – Lock the camera so that future MediaRecorder sessions can use it by calling Camera.lock(). 5)Stop the Preview – When your activity has finished using the camera, stop the preview using Camera.stopPreview(). 6)Release Camera – Release the camera so that other applications can use it by calling Camera.release(). 二、上代码,具体实现录制视频和视频播放功能 这里调用MediaRecorder的API实现视频录制功能并借用MediaPlayer多媒体播放类实现录制好的视频播放。 (1)布局文件如下,非常简单两个按钮下放置一个SurfaceView; 代码语言:javascript复制 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |