您的位置:首页 > 移动开发 > Android开发

【Android开发小记--15】录制视频

2016-02-07 14:39 435 查看
通过Intent 调用系统录制视频功能

使用VideoView 播放录制的视频

首先,设置布局文件,添加 button 和 videoView :

<?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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.dingding.recordervideo.MainActivity">
    <Button
        android:id="@+id/btn_recorder"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="调用系统录制视频功能" />
    <Button
        android:id="@+id/btn_play"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="播放视频"/>
    <VideoView
        android:id="@+id/videoView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"/>
</LinearLayout>


接着,在 MainActivity.java 中

通过Intent 调用系统录制视频功能;使用VideoView 播放录制的视频

1. 使用系统的 Uri 路径存储

2.使用自定义的 Uri 路径存储

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private Button btn_recorder;
    private Button btn_play;
    private VideoView videoView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_recorder = (Button) findViewById(R.id.btn_recorder);
        btn_play = (Button) findViewById(R.id.btn_play);
        videoView = (VideoView) findViewById(R.id.videoView);
        btn_recorder.setOnClickListener(this);
        btn_play.setOnClickListener(this);
    }
    private final int VIDEO_RECORDER = 1;
    private Uri outputUri;
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_recorder:
//------------------ 1 ----------------------------------------------
//                //调用系统录制视频程序
//                Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//                startActivityForResult(intent,VIDEO_RECORDER);
//------------------ 2 ----------------------------------------------
                //自定义录制的路径
                File file = new File(Environment.getExternalStorageDirectory(),"myRecorder.mp4");
                outputUri = Uri.fromFile(file);
                //调用系统录制视频程序
                Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT,outputUri);
                startActivityForResult(intent, VIDEO_RECORDER);
                break;
            case R.id.btn_play:
                videoView.setVideoURI(outputUri);
                videoView.start();
                break;
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode==RESULT_OK){
//------------------ 1 ----------------------------------------------
//            outputUri = data.getData();//获取到录制视频后的uri
        }
    }
}


注意添加读取外部文件权限:

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


具体代码点击

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: