实现效果以下
<?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_video" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.p_.VideoActivity"><SurfaceView android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/sfv_miand" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="开始录制" android:onClick="start" /><Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="停止录制" android:layout_marginLeft="260dp" android:onClick="stop" /></RelativeLayout>二:使用MediaRecorder类进行视频的录制
1.实例化媒体录制器
MediaRecorder=mediaRecorder = new MediaRecorder();2.重置
mediaRecorder.reset();3.设置视频和音频的来源
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);4.设置保存的格式(mp4格式)
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);5.设置编码格式
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);6.设置保存的路径
mediaRecorder.setOutputFile("mnt/sdcard/lx"+System.currentTimeMillis()+".mp4");7.将画面展示到SurfaceView
mediaRecorder.setPReviewDisplay(sfv_miand.getHolder().getSurface());8.准备
try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); }9.停止录制
mediaRecorder.stop(); mediaRecorder.reset();三:权限
权限一:获取写文件权限;
权限二:获取音频权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.RECORD_AUDIO"/>四:java文件具体代码
package com.example.p_;import android.media.MediaRecorder;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.SurfaceView;import android.view.View;import java.io.IOException;public class VideoActivity extends AppCompatActivity { private MediaRecorder mediaRecorder; private SurfaceView sfv_miand; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video); //获取 sfv_miand = (SurfaceView) findViewById(R.id.sfv_miand); //实例化媒体录制器 mediaRecorder = new MediaRecorder(); } //开始录制 public void start(View view){ //重置 mediaRecorder.reset(); //设置视频和音频的来源 mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); //设置保存的格式 mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); //设置编码格式 mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setVideoFrameRate(3); //设置保存的路径 mediaRecorder.setOutputFile("mnt/sdcard/lx"+System.currentTimeMillis()+".mp4"); //将画面展示到SurfaceView mediaRecorder.setPreviewDisplay(sfv_miand.getHolder().getSurface()); //准备 try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } } //停止录制 public void stop(View view){ mediaRecorder.stop(); mediaRecorder.reset(); }}记住步骤,想一下怎么实现,不要记住死代码。
新闻热点
疑难解答