安卓视频录像源码

这个demo非常简单,主要讲解了安卓怎样实现录像功能!适合初学者研究!
mainactivitv。java
package com.mycompany.myapp5;
 
import android.app.*;
import android.media.*;
import android.os.*;
import android.util.*;
import android.view.*;
import android.widget.*;
import java.io.*;
import java.io.File.*;
import android.database.Cursor;
import android.os.Environment;
public class MainActivity extends Activity {
public static final String TAG = "MainActivity";
@Override
protected void onDestroy() {
mediaRecorder.release();
super.onDestroy();
}
private SurfaceView surfaceView;
private Button btn_start;
private Button btn_stop;
private MediaRecorder mediaRecorder;
private boolean record;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mediaRecorder = new MediaRecorder();
surfaceView = (SurfaceView) findViewById(R.id.surfaceview);
this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.surfaceView.getHolder().setFixedSize(320, 240);//设置分辨率
btn_start = (Button) findViewById(R.id.btn_start);
btn_stop=(Button) findViewById(R.id.btn_stop);
ClickListener clickListener = new ClickListener();
btn_start.setOnClickListener(clickListener);
btn_stop.setOnClickListener(clickListener);
}
private final class ClickListener implements View.OnClickListener{
public void onClick(View v) {
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(MainActivity.this, "SD卡不存在或写保护!", 1).show();
return ;}
try {
switch(v.getId()){
case R.id.btn_start:
mediaRecorder.reset();
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); 
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoSize(640, 480);
mediaRecorder.setVideoFrameRate(3); //每秒3帧
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //设置视频编码方式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
File videoFile = new File(Environment.getExternalStorageDirectory(), "1.mp4");//设置存储文件名
mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
mediaRecorder.prepare();//预期准备
mediaRecorder.start();//开始刻录
record = true;
break;
case R.id.btn_stop:
if(record){
mediaRecorder.stop();
record = false;
}break;}
} catch (Exception e) {
Log.e(TAG, e.toString());
}}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >
 
    <SurfaceView       android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/surfaceview" />
<Button
android:id="@+id/btn_start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <Button
android:id="@+id/btn_stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
下面提供了源码下载地址

安卓视频录像源码

软件作者: admin

更新时间: 2014年08月17日
资源大小: 0KB 资源授权: 免费版
资源类别: 其它 所需点数: 0
资源语言: 简体中文 相关链接:
资源评级: 演示地址:
运行环境: android 下载地址1