录像操作

recoder.set的时候 已经要注意顺序否则有可能会导致初始化失败  下面直接上代码了

AndroidManifest.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3     package="cn.itcast.videorecoder"
 4     android:versionCode="1"
 5     android:versionName="1.0" >
 6 
 7     <uses-permission android:name="android.permission.RECORD_AUDIO" />
 8     <uses-permission android:name="android.permission.CAMERA" />
 9     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
10 
11     <uses-sdk android:minSdkVersion="4" />
12 
13     <application
14         android:icon="@drawable/ic_launcher"
15         android:label="@string/app_name" >
16         <activity
17             android:name=".DemoActivity"
18             android:label="@string/app_name" >
19             <intent-filter>
20                 <action android:name="android.intent.action.MAIN" />
21 
22                 <category android:name="android.intent.category.LAUNCHER" />
23             </intent-filter>
24         </activity>
25     </application>
26 
27 </manifest>

布局文件

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:layout_width="fill_parent"
 9         android:layout_height="wrap_content"
10         android:text="@string/hello" />
11 
12     <Button
13         android:id="@+id/button1"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:onClick="start"
17         android:text="开始录像" />
18 
19     <Button
20         android:id="@+id/button2"
21         android:layout_width="wrap_content"
22         android:layout_height="wrap_content"
23         android:onClick="stop"
24         android:text="停止录像" />
25     <Button
26         android:layout_width="wrap_content"
27         android:layout_height="wrap_content"
28         android:onClick="recodeBySystem"
29         android:text="调用系统的录像机 录像" />
30     <SurfaceView
31         android:id="@+id/sv"
32         android:layout_width="fill_parent"
33         android:layout_height="fill_parent" />
34 
35 </LinearLayout>

Activity文件

 1 package cn.itcast.videorecoder;
 2 
 3 import java.io.File;
 4 import java.io.IOException;
 5 
 6 import android.app.Activity;
 7 import android.content.Intent;
 8 import android.media.MediaRecorder;
 9 import android.media.MediaRecorder.AudioEncoder;
10 import android.media.MediaRecorder.AudioSource;
11 import android.media.MediaRecorder.OutputFormat;
12 import android.media.MediaRecorder.VideoEncoder;
13 import android.media.MediaRecorder.VideoSource;
14 import android.net.Uri;
15 import android.os.Bundle;
16 import android.provider.MediaStore;
17 import android.view.SurfaceHolder;
18 import android.view.SurfaceView;
19 import android.view.View;
20 import android.widget.Toast;
21 
22 public class DemoActivity extends Activity {
23     private MediaRecorder recoder;
24     private SurfaceView sv;
25     private SurfaceHolder holder;
26     @Override
27     public void onCreate(Bundle savedInstanceState) {
28         super.onCreate(savedInstanceState);
29         setContentView(R.layout.main);
30         sv = (SurfaceView) findViewById(R.id.sv);
31         holder = sv.getHolder();
32         holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
33 
34     }
35 
36     public void start(View view) {
37         try {
38             recoder = new MediaRecorder();
39             recoder.setAudioSource(AudioSource.MIC);
40             recoder.setVideoSource(VideoSource.CAMERA);
41             recoder.setOutputFormat(OutputFormat.THREE_GPP);
42             recoder.setAudioEncoder(AudioEncoder.DEFAULT);
43             recoder.setVideoEncoder(VideoEncoder.H264);
44             recoder.setOutputFile("/sdcard/a.3gp");
45             recoder.setVideoSize(640, 480);
46             recoder.setVideoFrameRate(5);
47             //指定预览界面
48             recoder.setPreviewDisplay(holder.getSurface());
49             recoder.prepare();
50             recoder.start();
51         } catch (Exception e) {
52             e.printStackTrace();
53         }
54     }
55 
56     public void stop(View view) {
57         if (recoder != null) {
58             recoder.stop();
59             recoder.release();
60             recoder = null;
61         }
62     }
63     
64     public void recodeBySystem(View  view){
65       /*  <intent-filter>
66         <action android:name="android.media.action.VIDEO_CAPTURE" />
67         <category android:name="android.intent.category.DEFAULT" />
68     </intent-filter>*/
69         Intent intent = new Intent();
70         intent.setAction("android.media.action.VIDEO_CAPTURE");
71         intent.addCategory("android.intent.category.DEFAULT");
72 //        Bundle extras = new Bundle();
73         File file = new File("/sdcard/aaa"+System.currentTimeMillis()+".3gp");
74 //        extras.putParcelable(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
75 //        intent.putExtras(extras);
76         intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(file));
77         startActivityForResult(intent, 0);
78     }
79     @Override
80     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
81         super.onActivityResult(requestCode, resultCode, data);
82         Toast.makeText(this, "录制完成", 0).show();
83     }
84 }
基础day07/videorecoder

 

posted @ 2013-01-19 19:37  王世桢  阅读(230)  评论(0编辑  收藏  举报