package com.video;
import java.io.File;
import java.io.IOException;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import android.graphics.PixelFormat;
import android.hardware.Camera;
public class RecorderVideo extends Activity implements SurfaceHolder.Callback
{
private String strTempFile = "User1_";
private File myRecAudioFile;
private File myRecAudioDir;
private Camera mCamera01;
private MediaRecorder recorder;
private SurfaceView surfaceView1 ;
private SurfaceHolder surfaceHolder;
private boolean isStopRecord;
private boolean sdCardExit;
private boolean recording = false;
private Button btn02,btn03,btn04;
private TextView tv01,tv02,tv03,tv04,tv05;
private int hour = 0;
private int minute = 0;
private int second = 0;
private boolean bool;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(null);
//全屏显示
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
//横屏显示
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.TRANSLUCENT);
setContentView(R.layout.main);
btn02 = (Button) findViewById(R.id.Button02);
btn03 = (Button) findViewById(R.id.Button03);
btn04 = (Button) findViewById(R.id.Button04);
tv01 = (TextView) findViewById(R.id.TextView01);
tv02 = (TextView) findViewById(R.id.TextView02);
tv03 = (TextView) findViewById(R.id.TextView03);
tv04 = (TextView) findViewById(R.id.TextView04);
tv05 = (TextView) findViewById(R.id.TextView05);
//设置TextView不可见
tv01.setVisibility(View.GONE);
tv02.setVisibility(View.GONE);
tv03.setVisibility(View.GONE);
tv04.setVisibility(View.GONE);
tv05.setVisibility(View.GONE);
// 判断SD Card是否插入
sdCardExit = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
// 如果sd卡存在,取得SD Card路径作为录音的文件位置
if(sdCardExit){
myRecAudioDir = Environment.getExternalStorageDirectory();
}else {
mMakeTextToast("存储卡不存在",true);
}
surfaceView1 = (SurfaceView) findViewById(R.id.SurfaceView);
//绑定surfaceView,取得surfaceHolder对象
surfaceHolder = surfaceView1.getHolder();
//activity必须实现surfaceHolder.Callback()
surfaceHolder.addCallback(RecorderVideo.this);
//设置显示器类型
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
//开始录制
btn02.setOnClickListener(mBtn02);
//停止录制
btn03.setOnClickListener(mBtn03);
//返回
btn04.setOnClickListener(mBtn04);
}
private Button.OnClickListener mBtn02 = new Button.OnClickListener(){
@Override
public void onClick(View v) {
//录制视频
recorder();
}
};
private Button.OnClickListener mBtn03 = new Button.OnClickListener(){
@Override
public void onClick(View v) {
//停止录制
stop();
}
};
private Button.OnClickListener mBtn04 = new Button.OnClickListener(){
@Override
public void onClick(View v) {
//返回
back();
}
};
/**
* 1.6以下版本需要初始化摄像头,即下面屏蔽的代码,
* 1.6以上的版本,初始化已经封装好,不需要初始化,否则会报错 prepared failed,但这样的话没有预览,还需要改进
*/
// public void initCamera(){
// if(!recording){
// mCamera01 = Camera.open();
// }
// if(mCamera01 != null && !recording){
// try {
// //创建Camera.Parameter对象
// Camera.Parameters parameters = mCamera01.getParameters();
// //设置Preview的尺寸
// parameters.setPreviewSize(640,480);
// //设置相机采用parameters
// mCamera01.setParameters(parameters);
// mCamera01.setPreviewDisplay(surfaceHolder);
// //立即运行Preview
// mCamera01.startPreview();
// recording = true;
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
//开始录像
public void recorder(){
try {
bool = true;
myRecAudioFile = File.createTempFile(strTempFile,".mp4",myRecAudioDir);
recorder = new MediaRecorder();
recorder.setPreviewDisplay(surfaceHolder.getSurface());
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//设置录音源为麦克风
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置输出格式为3gp
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
//设置视频大小
recorder.setVideoSize(480,320);
//每秒的帧数
recorder.setVideoFrameRate(24);
//设置视频编码
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
//设置音频编码
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(myRecAudioFile.getAbsolutePath());
recorder.prepare();
tv01.setVisibility(View.VISIBLE);
tv02.setVisibility(View.VISIBLE);
tv03.setVisibility(View.VISIBLE);
tv04.setVisibility(View.VISIBLE);
tv05.setVisibility(View.VISIBLE);
handler.postDelayed(task, 1000);
recorder.start();
recording = true;
isStopRecord = false;
} catch (IOException e) {
e.printStackTrace();
}
}
//停止录像
public void stop(){
if (recorder == null){
return;
}else{
recorder.stop();
bool = false;
tv01.setText(format(hour));
tv03.setText(format(minute));
tv05.setText(format(second));
recorder.release();
recording = false;
recorder = null;
mMakeTextToast("停止:" + myRecAudioFile.getName(),true);
// 停止录象
isStopRecord = true;
}
}
//返回
public void back(){
// if (recorder != null && !isStopRecord){
// //停止录象
// recorder.stop();
// recorder.release();
// }
finish();
}
//定时器设置,实现计时
private Handler handler = new Handler();
private Runnable task = new Runnable() {
public void run() {
if(bool){
handler.postDelayed(this, 1000);
second ++;
if(second < 60){
tv05.setText(format(second));
}else if(second < 3600){
minute = second / 60;
second = second % 60;
tv03.setText(format(minute));
tv05.setText(format(second));
}else{
hour = second / 3600;
minute = (second % 3600) / 60;
second = (second % 3600) % 60;
tv01.setText(format(hour));
tv03.setText(format(minute));
tv05.setText(format(second));
}
}
}
};
private void mMakeTextToast(String string, boolean b) {
if(b == true){
Toast.makeText(RecorderVideo.this, string, Toast.LENGTH_LONG).show();
} else {
Toast.makeText(RecorderVideo.this, string, Toast.LENGTH_SHORT).show();
}
}
//格式化时间
public String format(int i){
String s = i + "";
if(s.length() == 1){
s = "0" + s;
}
return s;
}
public void surfaceChanged(SurfaceHolder holder, int format, int width,int height) {
}
public void surfaceCreated(SurfaceHolder holder) {
}
public void surfaceDestroyed(SurfaceHolder holder) {
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
以前发的一个版本是在1.6版本上做的,很多人发现在1.6以上的版本运行都报错,经过查阅资料,得知1.6以上的版本将摄像头初始化的功能已经封装好了,所有在1.6以上的版本初始化后再录制会报错"prepared failed",将初始化的代码屏蔽掉,再处理一些设置就好了。 唯一不好的是,屏蔽掉初始化功能后就没有预览效果了,后期再改进一下。
资源推荐
资源详情
资源评论
收起资源包目录
RecorderVideo(1.6以上版本使用).zip (25个子文件)
RecorderVideo
bin
classes.dex 9KB
com
video
RecorderVideo$2.class 717B
RecorderVideo$1.class 721B
R$layout.class 367B
R$id.class 660B
R$drawable.class 373B
R$attr.class 313B
R.class 468B
RecorderVideo.class 7KB
RecorderVideo$3.class 717B
R$string.class 400B
RecorderVideo$4.class 2KB
RecorderVideo.apk 17KB
resources.ap_ 11KB
res
drawable-ldpi
icon.png 2KB
drawable-hdpi
icon.png 4KB
values
strings.xml 176B
drawable-mdpi
icon.png 3KB
layout
main.xml 2KB
assets
default.properties 364B
gen
com
video
R.java 1KB
src
com
video
RecorderVideo.java 7KB
.project 849B
.classpath 280B
AndroidManifest.xml 946B
共 25 条
- 1
资源评论
- staralone3692011-10-251.6以上版本还是有问题,没开始就挂掉了,应该不是相机初始化的问题
- lawesly2012-07-15在新版本Android 上不太好用,不能出来或者容易挂掉,兼容性问题?
鄙人姓胡
- 粉丝: 42
- 资源: 25
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功