首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android视频刻制

2013-02-24 
Android视频录制package cn.itcast.videorecordimport java.io.Fileimport android.app.Activityimport

Android视频录制

package cn.itcast.videorecord;import java.io.File;import android.app.Activity;import android.media.MediaRecorder;import android.os.Bundle;import android.os.Environment;import android.util.Log;import android.view.SurfaceHolder;import android.view.SurfaceView;import android.view.View;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {private static final String TAG = "MainActivity";    private SurfaceView surfaceView;    private MediaRecorder mediaRecorder;    private boolean record;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                mediaRecorder = new MediaRecorder();        surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);        /*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/        this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);        this.surfaceView.getHolder().setFixedSize(320, 240);//设置分辨率        ButtonClickListener listener = new ButtonClickListener();        Button stopButton = (Button) this.findViewById(R.id.stop);        Button recordButton = (Button) this.findViewById(R.id.record);        stopButton.setOnClickListener(listener);        recordButton.setOnClickListener(listener);            }        @Overrideprotected void onDestroy() {    mediaRecorder.release();super.onDestroy();}private final class ButtonClickListener implements View.OnClickListener{@Overridepublic void onClick(View v) {if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){Toast.makeText(MainActivity.this, R.string.sdcarderror, 1).show();return ;}try {switch (v.getId()) {case R.id.record:mediaRecorder.reset();mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);mediaRecorder.setVideoSize(320, 240);mediaRecorder.setVideoFrameRate(3); //每秒3帧mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263); //设置视频编码方式mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);File videoFile = new File(Environment.getExternalStorageDirectory(), System.currentTimeMillis()+".3gp");mediaRecorder.setOutputFile(videoFile.getAbsolutePath());mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());mediaRecorder.prepare();//预期准备mediaRecorder.start();//开始刻录record = true;break;case R.id.stop:if(record){mediaRecorder.stop();record = false;}break;}} catch (Exception e) {Toast.makeText(MainActivity.this, R.string.error, 1).show();Log.e(TAG, e.toString());}}        }}

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:background="#ffffff"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><SurfaceView    android:layout_width="fill_parent"     android:layout_height="220dip"     android:id="@+id/surfaceView"    />    <LinearLayout    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:gravity="right"    >    <Button    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="@string/record"    android:id="@+id/record"    />    <Button    android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_marginLeft="10dip"    android:text="@string/stop"    android:id="@+id/stop"    />    </LinearLayout></LinearLayout>

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="cn.itcast.videorecord"      android:versionCode="1"      android:versionName="1.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".MainActivity"                  android:label="@string/app_name" android:screenOrientation="landscape">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>    <uses-sdk android:minSdkVersion="7" /><uses-permission android:name="android.permission.RECORD_AUDIO"/> <uses-permission android:name="android.permission.CAMERA"/><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/></manifest> 

项目源码下载:http://pan.baidu.com/share/link?shareid=274384&uk=1796216265

热点排行