Android视录视频示例

Activity类:VideoActivity

package com.media.Media;    
    
import java.io.File;    
import java.io.IOException;    
    
import android.app.Activity;    
import android.media.MediaRecorder;    
import android.os.Bundle;    
import android.os.Environment;    
import android.view.SurfaceHolder;    
import android.view.SurfaceView;    
import android.view.View;    
import android.view.View.OnClickListener;    
import android.widget.Button;    
    
public class VideoActivity extends Activity {    
    
        
    private File myRecAudioFile;    
    private SurfaceView mSurfaceView;       
    private SurfaceHolder mSurfaceHolder;     
    private Button buttonStart;    
    private Button buttonStop;    
    private File dir;    
    private MediaRecorder recorder;    
        
        
    @Override    
    public void onCreate(Bundle savedInstanceState) {    
        super.onCreate(savedInstanceState);    
        setContentView(R.layout.video);    
        mSurfaceView = (SurfaceView) findViewById(R.id.videoView);       
        mSurfaceHolder = mSurfaceView.getHolder();       
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);     
        buttonStart=(Button)findViewById(R.id.start);    
        buttonStop=(Button)findViewById(R.id.stop);    
        File defaultDir = Environment.getExternalStorageDirectory();    
        String path = defaultDir.getAbsolutePath()+File.separator+"V"+File.separator;//创建文件夹存放视频    
        dir = new File(path);    
        if(!dir.exists()){    
            dir.mkdir();    
        }    
        recorder = new MediaRecorder();    
            
        buttonStart.setOnClickListener(new OnClickListener() {    
            @Override    
            public void onClick(View v) {    
                recorder();    
            }    
        });    
            
        buttonStop.setOnClickListener(new OnClickListener() {    
            @Override    
            public void onClick(View v) {    
                 recorder.stop();    
                 recorder.reset();    
                 recorder.release();    
                 recorder=null;    
            }    
        });    
    }    
        
        
        
    public void recorder() {    
        try {    
            myRecAudioFile = File.createTempFile("video", ".3gp",dir);//创建临时文件    
            recorder.setPreviewDisplay(mSurfaceHolder.getSurface());//预览    
            recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);//视频源    
            recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //录音源为麦克风    
            recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//输出格式为3gp    
            recorder.setVideoSize(800, 480);//视频尺寸    
            recorder.setVideoFrameRate(15);//视频帧频率    
            recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);//视频编码    
            recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//音频编码    
            recorder.setMaxDuration(10000);//最大期限    
            recorder.setOutputFile(myRecAudioFile.getAbsolutePath());//保存路径    
            recorder.prepare();    
            recorder.start();    
        } catch (IOException e) {    
            e.printStackTrace();    
        }    
    }    
}  

界面:video.xml

    <?xml version="1.0" encoding="utf-8"?>    
    <LinearLayout    
        xmlns:android="http://schemas.android.com/apk/res/android"    
        android:orientation="vertical"    
        android:layout_width="fill_parent"    
        android:layout_height="fill_parent">      
        <SurfaceView android:id="@+id/videoView"    
            android:visibility="visible"     
            android:layout_width="320px"     
            android:layout_height="240px">    
        </SurfaceView>    
            
        <RelativeLayout    
            android:layout_width="fill_parent"    
            android:layout_height="wrap_content">      
            <Button    
              android:layout_width="wrap_content"    
              android:layout_height="wrap_content"    
              android:text="录制"    
              android:id="@+id/start"/>      
            <Button    
              android:layout_width="wrap_content"    
              android:layout_height="wrap_content"    
              android:layout_toRightOf="@id/start"    
              android:text="停止"    
              android:id="@+id/stop"/>      
        </RelativeLayout>    
    </LinearLayout>   

权限配置:AndroidManifest.xml

    <?xml version="1.0" encoding="utf-8"?>    
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"    
          package="com.media.Media"    
          android:versionCode="1"    
          android:versionName="1.0">    
        <application android:icon="@drawable/rabbit" android:label="@string/app_name">    
            <activity android:name=".VideoActivity"    
                      android:label="@string/app_name">    
                <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.CAMERA"/>    
        <uses-permission android:name="android.permission.RECORD_AUDIO"/>    
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    
    </manifest>     

转帖:http://mobile.51cto.com/android-245935.htm
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值