一、配置文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.videorecord"
android:versionCode="1"
android:versionName="1.0" > <uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" /> <application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.videorecord.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-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>

  布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > <SurfaceView
android:id="@+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" /> <RelativeLayout
android:id="@+id/buttonlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
> <Button
android:id="@+id/videostop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="5dp"
android:layout_toLeftOf="@+id/videorecord"
android:onClick="record"
android:enabled="false"
android:text="@string/stop" /> <Button
android:id="@+id/videorecord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginRight="59dp"
android:onClick="record"
android:text="@string/record" />
</RelativeLayout> </RelativeLayout>

二、代码实现:

package com.example.videorecord;

import android.app.Activity;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
/**
* 视频采集刻录
* @author Administrator
*
*/
public class MainActivity extends Activity {
private View layout;
private SurfaceView surfaceView;
private Button recordButton;
private Button stopButton;
private MediaRecorder recorder;
private boolean record=false; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置窗口没有标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
//全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); setContentView(R.layout.activity_main);
layout= this.findViewById(R.id.buttonlayout);
recordButton= (Button) this.findViewById(R.id.videorecord);
stopButton= (Button) this.findViewById(R.id.videostop);
//确定surfaceView什么时候被时间
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
/*下面设置Surface不维护自己的缓冲区,而是等待屏幕的渲染引擎将内容推送到用户面前*/
surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceView.getHolder().setFixedSize(176, 144); //设置分辨率
surfaceView.getHolder().setKeepScreenOn(true);//设置高亮 }
public void record(View v){
try {
switch (v.getId()) {
case R.id.videorecord:
recorder = new MediaRecorder();
recorder.reset();
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //从照相机采集视频
recorder.setAudioSource(MediaRecorder.AudioSource.MIC); //麦克风
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);//输出格式3gp
recorder.setVideoSize(320, 240);//视频大小
recorder.setVideoFrameRate(3); //每秒3帧,每秒从摄像头捕获多少画面
recorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //设置视频编码方式
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置声音编码
recorder.setOutputFile("/storage/sdcard0/360/"+System.currentTimeMillis()+".3gp");//文件输出位置
//设置预览显示
recorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
recorder.prepare();//预期准备
recorder.start();//开始刻录
record = true;
recordButton.setEnabled(false);
stopButton.setEnabled(true);
break;
case R.id.videostop:
if(record){
recorder.stop();//停止刻录
record = false;
recorder.release();
recorder=null;
}
recordButton.setEnabled(true);
stopButton.setEnabled(false);
break;
}
} catch (Exception e) {
e.printStackTrace();
}
} //触摸事件
public boolean onTouchEvent(MotionEvent event) {
//手指按下屏幕
if(event.getAction()==MotionEvent.ACTION_DOWN){
layout.setVisibility(ViewGroup.VISIBLE);//显示布局
return true;
}
return super.onTouchEvent(event);
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }

最新文章

  1. 关于Simple_html_dom的小应用
  2. md5应用
  3. [Docker]Docker快速上手学习笔记
  4. javascript类型注意事项
  5. Windows Store App 用户库文件夹操作
  6. TCP状态变迁流程
  7. 微软Asp.net MVC5生命周期流程图
  8. 济南学习 Day 2 T2 pm
  9. 添加删除ASM磁盘
  10. JSONP(处理跨域问题)
  11. 共享式以太网与交换式以太网的性能比较(OPNET网络仿真实验)
  12. 再回首,Java温故知新(十):数组
  13. poj1418 Viva Confetti 判断圆是否可见
  14. android插件化-apkplugdemo源代码阅读指南-10
  15. dedecms的热门标签在那里修改
  16. Java获取Linux和Window系统CPU、内存和磁盘总使用率的情况
  17. C# Quartz定时任务corn时间设置详解
  18. Fiddler 会话过滤功能
  19. 04 jsp,EL,JSTL
  20. MySQL order by的实现

热门文章

  1. 【CAD】创建多行文本
  2. socket 客户端和服务端通信
  3. hdu 3255 体积并
  4. c# 字体库跨域解决
  5. springboot 整合kafka
  6. JavaScirpt(JS)的this细究
  7. 1729 单词查找树 2000年NOI全国竞赛
  8. linux环境下安装jdk(本文示例是jdk1.6.0_45)
  9. H5手机页面剖析
  10. Resharper安装及激活--转载