一直对录制视频感兴趣,最近项目中遇到了,就顺便学习了下,话不多说,直接上代码
RecordVideoActivity.class
package com.czh.tongjiproject.ui; import android.os.Bundle; import android.os.CountDownTimer; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.czh.tongjiproject.base.BaseActivity; import com.czh.tongjiproject.basenet.OkHttpUtil; import com.czh.tongjiproject.util.UrlManager; import com.czh.tongjiproject.widget.BackDialog; import com.czh.tongjiproject.widget.MyVideoView; import java.io.IOException; import czh.com.tongjiproject.R; import okhttp3.Call; import okhttp3.Response; /** * 录制视频界面 */ public class RecordVideoActivity extends BaseActivity implements View.OnClickListener,OkHttpUtil.OkHttpCallBack { private TextView act_record_video_time;//倒计时时间 private Button act_record_video_recordBtn;//录制按钮 private MyVideoView myVideoView; private MyCount mc; private String filePath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_record_video); initView();//初始化布局控件 } /*定义一个倒计时的内部类*/ class MyCount extends CountDownTimer { public MyCount(long millisInFuture, long countDownInterval) { super(millisInFuture, countDownInterval); } @Override public void onFinish() { filePath = myVideoView.getFilePath(); act_record_video_time.setVisibility(View.GONE); myVideoView.endRecord(); act_record_video_recordBtn.setClickable(true); act_record_video_recordBtn.setText("开始上传"); } @Override public void onTick(long millisUntilFinished) { act_record_video_time.setText("倒计时:" + (millisUntilFinished / 1000) + "秒"); } } private void initView() { mc = new MyCount(15000, 1000); myVideoView = fView(R.id.myVideoView); common_head_back = fView(R.id.common_head_back);//返回键 act_record_video_time = fView(R.id.act_record_video_time);//倒计时时间 common_head_back.setVisibility(View.VISIBLE); act_record_video_recordBtn = fView(R.id.act_record_video_recordBtn); onClickListener(); } private void onClickListener() { common_head_back.setOnClickListener(this); act_record_video_recordBtn.setOnClickListener(this); act_record_video_recordBtn.setClickable(true); } @Override public void onClick(View view) { switch (view.getId()) { case R.id.act_record_video_recordBtn: if (isEmpty(filePath)) {//如果路径为空,那么点击录制 btnDaoJiShi(); } else {//路径不为空,等待上传 Toast.makeText(RecordVideoActivity.this, "正在上传,请稍后...", Toast.LENGTH_SHORT).show(); } break; } } public void btnDaoJiShi() { mc.start(); myVideoView.startRecord();//开始录制视频 act_record_video_recordBtn.setText("录制中"); act_record_video_recordBtn.setClickable(false); } }activiity_record_video.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <include layout="@layout/common_head" /> <RelativeLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="3.5"> <com.czh.tongjiproject.widget.MyVideoView android:id="@+id/myVideoView" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="2" android:background="@color/gray" android:gravity="center" android:orientation="vertical"> <TextView android:id="@+id/act_record_video_time" style="@style/recordtvsytle" android:text="倒计时:15秒" /> <Button android:id="@+id/act_record_video_recordBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/login_but" android:paddingLeft="30dp" android:paddingRight="30dp" android:text="@string/start_record" android:textColor="@android:color/white" android:textSize="14sp" android:textStyle="bold" /> </LinearLayout> </LinearLayout>MyViedoView.class
package com.czh.tongjiproject.widget; import android.content.Context; import android.graphics.PixelFormat; import android.hardware.Camera; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.media.MediaPlayer.OnErrorListener; import android.media.MediaRecorder; import android.os.CountDownTimer; import android.os.Environment; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.view.animation.AlphaAnimation; import android.view.animation.Animation; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.Toast; import java.io.File; import java.io.IOException; import czh.com.tongjiproject.R; /** * Created by Administrator on 2016/7/19. * Jiahuan */ public class MyVideoView extends FrameLayout implements SurfaceHolder.Callback, View.OnClickListener { private String filePath; private View view; private SurfaceView act_record_video_surfaceView;//显示视频的控件 private SurfaceHolder surfaceHolder;// 用来显示视频的一个接口 private MediaRecorder recorder;// 录制视频的类 private LinearLayout widget_myvideovide_ll; private Camera camera; public MyVideoView(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; view = LayoutInflater.from(context).inflate( R.layout.widget_myvideoview, this, true); findView(); } private File createFile() { File defaultDir = Environment.getExternalStorageDirectory(); String path = defaultDir.getAbsolutePath() + File.separator + "V" + File.separator;//创建文件夹存放视频 File dir = new File(path); if (!dir.exists()) { dir.mkdir(); } return dir; } private void findView() { mc = new MyCount(15000, 500); // recorder = new MediaRecorder(); act_record_video_surfaceView = (SurfaceView) view.findViewById(R.id.act_record_video_surfaceView); act_record_video_surfaceView.setZOrderOnTop(true);//设置画布 背景透明 act_record_video_surfaceView.getHolder().setFormat(PixelFormat.TRANSLUCENT); surfaceHolder = act_record_video_surfaceView.getHolder();// 取得holder surfaceHolder.addCallback(this); // holder加入回调接口 // setType必须设置,要不出错. surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); act_record_video_videoView = (VideoViewWidget) view.findViewById(R.id.act_record_video_videoView); act_record_video_videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mediaPlayer) { seekBar.setMax(mediaPlayer.getDuration()); } }); widget_myvideovide_ll = (LinearLayout) view.findViewById(R.id.widget_myvideovide_ll); } /** * 开始录制的方法 */ private void recordVideoStart() { try { recorder = new MediaRecorder(); File myRecAudioFile = File.createTempFile("video", ".mp4", createFile());//创建临时文件 filePath = myRecAudioFile.getAbsolutePath();//获取绝对路径 // 设置录制视频源为Camera(相机) camera = Camera.open(Camera.CameraInfo.CAMERA_FACING_BACK); if (camera != null) { camera.setDisplayOrientation(90);// 摄像图旋转90度 camera.unlock(); recorder.setCamera(camera); } recorder.setPreviewDisplay(surfaceHolder.getSurface());//预览 recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//视频源 recorder.setOrientationHint(90);//视频旋转90度 recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //录音源为麦克风 recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//输出格式为3gp // recorder.setVideoSize(800, 480);//视频尺寸 recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);//视频编码 recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频编码 recorder.setMaxDuration(10000);//最大期限 recorder.setOutputFile(myRecAudioFile.getAbsolutePath());//保存路径 recorder.prepare(); recorder.start(); } catch (IOException e) { e.printStackTrace(); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { // 将holder,这个holder为开始在onCreate里面取得的holder,将它赋给surfaceHolder surfaceHolder = holder; } @Override public void surfaceCreated(SurfaceHolder holder) { // 将holder,这个holder为开始在onCreate里面取得的holder,将它赋给surfaceHolder surfaceHolder = holder; } @Override public void surfaceDestroyed(SurfaceHolder holder) { // surfaceDestroyed的时候同时对象设置为null act_record_video_surfaceView = null; surfaceHolder = null; recorder = null; } /** * 开始录制 */ public void startRecord() { act_record_video_surfaceView.setVisibility(View.VISIBLE); recordVideoStart(); } /** * 结束录制 */ public String endRecord() { if (recorder != null) { // 停止录制 recorder.stop(); // 释放资源 recorder.reset(); recorder.release(); recorder = null; camera.stopPreview(); camera.release(); camera = null; } if (act_record_video_surfaceView != null) { act_record_video_surfaceView.setVisibility(View.GONE); } return filePath; } public String getFilePath() { return filePath; } //1是录制 public int getCurrentShow() { return (this.act_record_video_surfaceView.getVisibility() == View.VISIBLE) ? 2 : 1; } }widget_myvideoview.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <SurfaceView android:id="@+id/act_record_video_surfaceView" android:layout_width="match_parent" android:layout_height="match_parent" android:visibility="visible" android:backgroundTint="@android:color/white" /> </FrameLayout>
好了,简单的就写完了