首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

小弟我的Android进阶之旅->Android视频录制小例子

2013-09-28 
我的Android进阶之旅------Android视频录制小例子首先看看官网上关于视频捕

我的Android进阶之旅------>Android视频录制小例子



============================首先看看官网上关于视频捕捉的介绍================================

Capturing videos

Video capture using the Android framework requires careful management of the Camera object and coordination with the MediaRecorder class. When recording video with Camera, you must manage the Camera.lock() andCamera.unlock() calls to allow MediaRecorder access to the camera hardware, in addition to the Camera.open()and Camera.release() calls.

Note: Starting with Android 4.0 (API level 14), the Camera.lock() and Camera.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.

  1. Open Camera - Use the Camera.open() to get an instance of the camera object.
  2. Connect Preview - Prepare a live camera image preview by connecting a SurfaceView to the camera usingCamera.setPreviewDisplay().
  3. Start Preview - Call Camera.startPreview() to begin displaying the live camera images.
  4. Start Recording Video - The following steps must be completed in order to successfully record video:
    1. Unlock the Camera - Unlock the camera for use by MediaRecorder by calling Camera.unlock().
    2. Configure MediaRecorder - Call in the following MediaRecorder methods in this order. For more information, see the MediaRecorder reference documentation.
      1. setCamera() - Set the camera to be used for video capture, use your application's current instance ofCamera.
      2. setAudioSource() - Set the audio source, use MediaRecorder.AudioSource.CAMCORDER.
      3. setVideoSource() - Set the video source, use MediaRecorder.VideoSource.CAMERA.
      4. Set the video output format and encoding. For Android 2.2 (API Level 8) and higher, use theMediaRecorder.setProfile method, and get a profile instance using CamcorderProfile.get(). For versions of Android prior to 2.2, you must set the video output format and encoding parameters:
        1. setOutputFormat() - Set the output format, specify the default setting orMediaRecorder.OutputFormat.MPEG_4.
        2. setAudioEncoder() - Set the sound encoding type, specify the default setting orMediaRecorder.AudioEncoder.AMR_NB.
        3. setVideoEncoder() - Set the video encoding type, specify the default setting orMediaRecorder.VideoEncoder.MPEG_4_SP.
      5. setOutputFile() - Set the output file, use getOutputMediaFile(MEDIA_TYPE_VIDEO).toString() from the example method in the Saving Media Files section.
      6. setPreviewDisplay() - Specify the SurfaceView preview layout element for your application. Use the same object you specified for Connect Preview.

      Caution: You must call these MediaRecorder configuration methods in this order, otherwise your application will encounter errors and the recording will fail.

    3. Prepare MediaRecorder - Prepare the MediaRecorder with provided configuration settings by callingMediaRecorder.prepare().
    4. Start MediaRecorder - Start recording video by calling MediaRecorder.start().
  5. Stop Recording Video - Call the following methods in order, to successfully complete a video recording:
    1. Stop MediaRecorder - Stop recording video by calling MediaRecorder.stop().
    2. Reset MediaRecorder - Optionally, remove the configuration settings from the recorder by callingMediaRecorder.reset().
    3. Release MediaRecorder - Release the MediaRecorder by calling MediaRecorder.release().
    4. Lock the Camera - Lock the camera so that future 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.
  6. Stop the Preview - When your activity has finished using the camera, stop the preview usingCamera.stopPreview().
  7. Release Camera - Release the camera so that other applications can use it by calling Camera.release().

Note: It is possible to use MediaRecorder 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, set setRecordingHint(boolean) to true prior to starting your preview. This setting can help reduce the time it takes to start recording.


============================再看看官网上关于音频捕捉的介绍================================

Audio Capture

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 the MediaRecorder 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.

Performing Audio Capture

Audio capture from the device is a bit more complicated than audio and video playback, but still fairly simple:

  1. Create a new instance of android.media.MediaRecorder.
  2. Set the audio source using MediaRecorder.setAudioSource(). You will probably want to useMediaRecorder.AudioSource.MIC.
  3. Set output file format using MediaRecorder.setOutputFormat().
  4. Set output file name using MediaRecorder.setOutputFile().
  5. Set the audio encoder using MediaRecorder.setAudioEncoder().
  6. Call MediaRecorder.prepare() on the MediaRecorder instance.
  7. To start audio capture, call MediaRecorder.start().
  8. To stop audio capture, call MediaRecorder.stop().
  9. When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. CallingMediaRecorder.release() is always recommended to free the resource immediately.


下面就看看该小例子的代码吧。

文件1.该应用的布局文件,res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"package="cn.oyp.recoder" android:versionCode="1" android:versionName="1.0"><uses-sdk android:minSdkVersion="8" /><!-- 摄像头权限 --><uses-permission android:name="android.permission.CAMERA" /><!-- 录制音频权限 --><uses-permission android:name="android.permission.RECORD_AUDIO"/><!-- 在SD卡中创建和删除文件权限 --><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><!-- 往SD卡中写入数据的权限 --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><application android:icon="@drawable/icon" android:label="@string/app_name"><activity android:name=".RecoderActivity" 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></manifest>

=================================================================================================

  作者:欧阳鹏  欢迎转载,与人分享是进步的源泉!

  转载请保留原文地址:http://blog.csdn.net/ouyang_peng

==================================================================================================

热点排行