Android实现录制视频

先展示下效果图:
1.这个是调到系统录制视频界面

VideoActivity.java

package com.example.videodisplay;

import android.content.Intent;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class VideoActivity extends AppCompatActivity {

    private Button bt_1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video);
        bt_1 = (Button) findViewById(R.id.bt_1);
        bt_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =  new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY,  1);
                intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT,  61);

                //这里1024 * 1024 * 100是指限制100M,当达到是会自动停止录制
                intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT,  1024 * 1024 * 100);
                startActivityForResult(intent,  11);
            }
        });
    }
}

activity_video.xml


<LinearLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.videodisplay.VideoActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="跳转至录制视频"
        android:id="@+id/bt_1"
        />
LinearLayout>

2.这个是使用mediaRecorder实现的

Video2Activity .java

package com.example.videodisplay;

import android.media.MediaPlayer;
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 Video2Activity extends AppCompatActivity {

    private SurfaceView sfv;
    private MediaRecorder mediaRecorder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video2);
        sfv = (SurfaceView) findViewById(R.id.sv_media_recoder_surface);

        //实例化MediaRecorder
        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.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
        mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);

        //设置保存的路径
         mediaRecorder.setOutputFile("mnt/sdcard//360Video/Video_"+System.currentTimeMillis()+".mp4");
         mediaRecorder.setPreviewDisplay(sfv.getHolder().getSurface());

        try {
            mediaRecorder.prepare();
            mediaRecorder.start();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    public void stop(View view){
        mediaRecorder.stop();
    }

}

activity_video2.xml


<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    tools:context="com.example.videodisplay.Video2Activity">

    <SurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/sv_media_recoder_surface"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="开始"
            android:onClick="start"
            android:layout_weight="1"
            />
        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="停止"
            android:onClick="stop"
            android:layout_weight="1"
            />
    LinearLayout>
RelativeLayout>

权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

你可能感兴趣的:(Android)