本文章主要将通过意图触发内置的Camera应用程序来录制视频。
源代码:
布局文件:
activity_main:
<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" > <Button android:id="@+id/button_capture" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="录制视频" /> <Button android:id="@+id/button_play" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button_capture" android:text="播放视频" /> <VideoView android:id="@+id/videoView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button_play" /> </RelativeLayout>
MainActivity:
package com.multimediademo14intentcamera; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.VideoView; /** * 使用意图录制视频 * */ public class MainActivity extends Activity implements OnClickListener { public static int VIDEO_CAPTURED = 1; /** * 两个按钮: 录制视频按钮、播放视频按钮 */ private Button button_capture, button_play; private VideoView videoView; private Uri videoFileUri; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button_capture = (Button) findViewById(R.id.button_capture); button_play = (Button) findViewById(R.id.button_play); videoView = (VideoView) findViewById(R.id.videoView); button_capture.setOnClickListener(this); button_play.setOnClickListener(this); button_play.setEnabled(false); } @Override public void onClick(View v) { if (v == button_capture) { /** * 店家录制视频按钮,将创建意图并连同VIDEO_CAPTURED常量一起将它传递给startActivityForResult方法, * 这将启动内置的Camera应用程序。 */ Intent captureVideoIntent = new Intent( android.provider.MediaStore.ACTION_VIDEO_CAPTURE); startActivityForResult(captureVideoIntent, VIDEO_CAPTURED); } else if (v == button_play) { videoView.setVideoURI(videoFileUri); videoView.start(); } } /** * 当Camera应用程序返回时,将调用如下的onActivityResult方法。它将检查resultCode是否为常量RESULT_OK, * 以及requestCode是否为传入到startActivityForResult的值VIDEO_CAPTURED * ,然后获取录制的视频文件的Uri。 */ @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == RESULT_OK && requestCode == VIDEO_CAPTURED) { videoFileUri = data.getData(); button_play.setEnabled(true); } } }
Android 使用意图录制视频,布布扣,bubuko.com
原文地址:http://blog.csdn.net/u012440207/article/details/27540189