============================首先看看官网上关于视频捕捉的介绍================================
Video capture using the Android framework requires careful management of theCamera
object and coordination with theMediaRecorder
class. When recording video withCamera
, you must manage theCamera.lock()
andCamera.unlock()
calls to allowMediaRecorder
access to the camera hardware, in addition to theCamera.open()
andCamera.release()
calls.
Note:Starting with Android 4.0 (API level 14), theCamera.lock()
andCamera.unlock()
calls are managed for you automatically.
Unlike taking pictures with a device camera, capturing video requires a very particular call order. You must follow a specific order of execution to successfully prepare for and capture video with your application, as detailed below.
Camera.open()
to get an instance of the camera object.SurfaceView
to the camera usingCamera.setPreviewDisplay()
.Camera.startPreview()
to begin displaying the live camera images.MediaRecorder
by callingCamera.unlock()
.MediaRecorder
methodsin this order. For more information, see theMediaRecorder
reference documentation.
setCamera()
- Set the camera to be used for video capture, use your application's current instance ofCamera
.setAudioSource()
- Set the audio source, useMediaRecorder.AudioSource.CAMCORDER
.setVideoSource()
- Set the video source, useMediaRecorder.VideoSource.CAMERA
.MediaRecorder.setProfile
method, and get a profile instance usingCamcorderProfile.get()
. For versions of Android prior to 2.2, you must set the video output format and encoding parameters:
setOutputFormat()
- Set the output format, specify the default setting orMediaRecorder.OutputFormat.MPEG_4
.setAudioEncoder()
- Set the sound encoding type, specify the default setting orMediaRecorder.AudioEncoder.AMR_NB
.setVideoEncoder()
- Set the video encoding type, specify the default setting orMediaRecorder.VideoEncoder.MPEG_4_SP
.setOutputFile()
- Set the output file, usegetOutputMediaFile(MEDIA_TYPE_VIDEO).toString()
from the example method in theSaving Media Filessection.setPreviewDisplay()
- Specify theSurfaceView
preview layout element for your application. Use the same object you specified forConnect Preview. Caution:You must call theseMediaRecorder
configuration methodsin this order, otherwise your application will encounter errors and the recording will fail.
MediaRecorder
with provided configuration settings by callingMediaRecorder.prepare()
.MediaRecorder.start()
.MediaRecorder.stop()
.MediaRecorder.reset()
.MediaRecorder
by callingMediaRecorder.release()
.MediaRecorder
sessions can use it by callingCamera.lock()
. Starting with Android 4.0 (API level 14), this call is not required unless theMediaRecorder.prepare()
call fails.Camera.stopPreview()
.Camera.release()
. Note:It is possible to useMediaRecorder
without creating a camera preview first and skip the first few steps of this process. However, since users typically prefer to see a preview before starting a recording, that process is not discussed here.
Tip:If your application is typically used for recording video, setsetRecordingHint(boolean)
totrue
prior to starting your preview. This setting can help reduce the time it takes to start recording.
============================再看看官网上关于音频捕捉的介绍================================
The Android multimedia framework includes support for capturing and encoding a variety of common audio formats, so that you can easily integrate audio into your applications. You can record audio using theMediaRecorder
APIs if supported by the device hardware.
This document shows you how to write an application that captures audio from a device microphone, save the audio and play it back.
Note:The Android Emulator does not have the ability to capture audio, but actual devices are likely to provide these capabilities.
Audio capture from the device is a bit more complicated than audio and video playback, but still fairly simple:
android.media.MediaRecorder
.MediaRecorder.setAudioSource()
. You will probably want to useMediaRecorder.AudioSource.MIC
.MediaRecorder.setOutputFormat()
.MediaRecorder.setOutputFile()
.MediaRecorder.setAudioEncoder()
.MediaRecorder.prepare()
on the MediaRecorder instance.MediaRecorder.start()
.MediaRecorder.stop()
.MediaRecorder.release()
on it. CallingMediaRecorder.release()
is always recommended to free the resource immediately.
文件1.该应用的布局文件,res/layout/main.xml
Hello World, RecoderActivity!
视频刻录小例子
刻录
停止
检测到手机没有存储卡!请插入手机存储卡再开启本应用
已经达到最长录制时间
package cn.oyp.recoder;
import java.io.File;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnInfoListener;
import android.os.Bundle;
import android.os.Environment;
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;
import android.widget.RelativeLayout;
import android.widget.Toast;
public class RecoderActivity extends Activity {
// 用来显示图片
private SurfaceView surfaceView;
// 刻录和停止按钮布局
private RelativeLayout buttonlayout;
// 刻录按钮
private Button recoderbutton;
// 停止按钮
private Button stopbutton;
// 媒体刻录对象
private MediaRecorder mediaRecorder;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 窗口特效为无标题
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 设置窗口全屏
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// 设定屏幕显示为横向
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.main);
buttonlayout = (RelativeLayout) this.findViewById(R.id.buttonlayout);
recoderbutton = (Button) this.findViewById(R.id.recoderbutton);
stopbutton = (Button) this.findViewById(R.id.stopbutton);
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
// 获取的画面直接输出到屏幕上
surfaceView.getHolder()
.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// 画面分辨率
surfaceView.getHolder().setFixedSize(176, 144);
// 保持屏幕高亮
surfaceView.getHolder().setKeepScreenOn(true);
}
// 点击刻录按钮处理方法
public void recoder(View v) {
try {
// 判断是否存在SD卡
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED)) {
// 将刻录的视频保存到SD卡中
File videoFile = new File(
Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".3gp");
mediaRecorder = new MediaRecorder();
// 设置声音采集来源于麦克风
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
// 设置视频采集来源于摄像头
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
// 设置输出格式为3gp
mediaRecorder
.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
// 设置视频尺寸
mediaRecorder.setVideoSize(surfaceView.getWidth(),
surfaceView.getHeight());
// 设置每秒钟捕捉画面个数为5帧
mediaRecorder.setVideoFrameRate(5);
// 设置声音编码
mediaRecorder
.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
// 设置视频编码
mediaRecorder.setAudioEncoder(MediaRecorder.VideoEncoder.H264);
// 设置视频的最大持续时间
mediaRecorder.setMaxDuration(10000);
mediaRecorder.setOnInfoListener(new OnInfoListener() {
@Override
public void onInfo(MediaRecorder mr, int what, int extra) {
if (what == MediaRecorder.MEDIA_RECORDER_INFO_MAX_DURATION_REACHED) {
Toast.makeText(getApplicationContext(),
R.string.maxDuration, Toast.LENGTH_LONG)
.show();
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
}
}
});
// 设置刻录的视频保存路径
mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
// 设置预览显示
mediaRecorder.setPreviewDisplay(surfaceView.getHolder()
.getSurface());
// 预期准备
mediaRecorder.prepare();
// 开始刻录
mediaRecorder.start();
} else {
Toast.makeText(getApplicationContext(), R.string.noSDcard,
Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
// 刻录按钮不可点击
recoderbutton.setEnabled(false);
// 停止按钮可点击
stopbutton.setEnabled(true);
}
// 点击停止按钮处理方法
public void stop(View v) {
// 停止刻录,并释放资源
if (mediaRecorder != null) {
mediaRecorder.stop();
mediaRecorder.release();
mediaRecorder = null;
}
// 刻录按钮可点击
recoderbutton.setEnabled(true);
// 停止按钮不可点击
stopbutton.setEnabled(false);
}
/** 当触摸屏幕的时候,将对焦和拍照按钮布局显示出来 */
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
buttonlayout.setVisibility(ViewGroup.VISIBLE);
return true;
}
return super.onTouchEvent(event);
}
}
=================================================================================================
作者:欧阳鹏 欢迎转载,与人分享是进步的源泉!
转载请保留原文地址:http://blog.csdn.net/ouyang_peng
==================================================================================================