android studio 录像
时间: 2023-11-17 12:03:35 浏览: 122
Androi Studi提供了许多API和框架来实现视频录制功能。以下是一个基本的录像功能的实现步骤:\n\1. 确保在AndroiManifes.xm文件中添加了相机和麦克风权限。\n\2. 在布局文件中添加一个SurfView元素,用于显示摄像头捕获的视频。\n\3. 在Activity中获取SurfView并将其设置为摄像头预览的目标。\n\4. 实例化MediRecorder类并设置视频源,输出格式,编码器,输出文件路径等参数。\n\5. 在按钮单击事件中启动MediRecorder并开始录制视频。\n\6. 在停止按钮单击事件中停止MediRecorder并释放资源。\n\以下是一个基本的录像功能的代码实现:\n\```jav\publi class MainActivity extends AppCompatActivity implements SurfHolder.Callback {\n\ priv Camer mCamer;\ priv SurfView mSurfView;\ priv SurfHolder mHolder;\ priv MediRecorder mMediRecorder;\ priv b isRecording = fals;\n\ @Overri\ pr voi onCr(Bu savedInstanceS) {\ super.onCr(savedInstanceS);\ setContentView(R.layou.activity_mai);\n\ mSurfView = (SurfView) findViewByI(R.i.surfView);\ mHolder = mSurfView.gHolder();\ mHolder.Callback(this);\ }\n\ @Overri\ publi voi surfCr(SurfHolder holder) {\ mCamer = Camer.op();\ mCamer.sDisplayOrientati(90);\ try {\ mCamer.sPreviewDisplay(holder);\ mCamer.starPreview();\ } catch (IOExcepti ) {\ .printStackTr();\ }\ }\n\ @Overri\ publi voi surfChang(SurfHolder holder, i form, i width, i heigh) {\n\ }\n\ @Overri\ publi voi surfDestroy(SurfHolder holder) {\ mCamer.stopPreview();\ mCamer.releas();\ mCamer = nu;\ }\n\ publi voi starRecording(View view) {\ if (isRecording) {\ retur;\ }\ try {\ mMediRecorder = new MediRecorder();\ mCamer.unlock();\ mMediRecorder.sCamer(mCamer);\ mMediRecorder.sAudiSour(MediRecorder.AudiSour.CAMCORDER);\ mMediRecorder.sViSour(MediRecorder.ViSour.CAMERA);\ mMediRecorder.setOutpuForm(MediRecorder.OutpuForm.MPEG_4);\ mMediRecorder.sAudiEncoder(MediRecorder.AudiEncoder.AAC);\ mMediRecorder.sViEncoder(MediRecorder.ViEncoder.H264);\ mMediRecorder.setOutpuFi(gOutpuMediFi().String());\ mMediRecorder.sPreviewDisplay(mHolder.gSurf());\ mMediRecorder.prepar();\ mMediRecorder.star();\ isRecording = tru;\ } catch (IOExcepti ) {\ .printStackTr();\ }\ }\n\ publi voi stopRecording(View view) {\ if (!isRecording) {\ retur;\ }\ mMediRecorder.stop();\ mMediRecorder.res();\ mMediRecorder.releas();\ mMediRecorder = nu;\ mCamer.lock();\ isRecording = fals;\ }\n\ priv stati Fi gOutpuMediFi() {\ Fi mediStoragDir = new Fi(Environm.getExternalStoragPubliDirectory(\ Environm.DIRECTORY_MOVIES), \MyCamerApp\");\ if (!mediStoragDir.exists()) {\ if (!mediStoragDir.mkdirs()) {\ retur nu;\ }\ }\ String timeStamp = new SimpleDateForm(\yyyyMM_HHmmss\").form(new D());\ Fi mediFi = new Fi(mediStoragDir.getPath() + Fi.separator +\ \VID_\ + timeStamp + \".mp4\");\ retur mediFi;\ }\}\```\n\
阅读全文