【翻译】(86)音频捕捉
see
http://developer.android.com/guide/topics/media/audio-capture.html
原文见
http://developer.android.com/guide/topics/media/audio-capture.html
-------------------------------
Audio Capture
音频捕捉
-------------------------------
In this document
本文目录
* Performing Audio Capture 执行音频捕捉
* Code Example 代码示例
Key classes
关键类
MediaRecorder
See also
另见
Android Supported Media Formats Android支持的媒体格式
Data Storage 数据存储
MediaPlayer
-------------------------------
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.
Android多媒体框架包含对捕捉和编码各种通用音频格式的支持,使你可以轻易地集成音频进你的应用程序中。你可以使用MediaRecorder的API记录音频,如果它被设备硬件支持。
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.
注意:Android模拟器没有能力捕捉音频,但实际设备多半提供这些功能。
-------------------------------
-------------------------------
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.
1. 创建一个android.media.MediaRecorder的新实例。
2. Set the audio source using MediaRecorder.setAudioSource(). You will probably want to use MediaRecorder.AudioSource.MIC.
2. 使用MediaRecorder.setAudioSource()设置音频源。你将很可能希望使用MediaRecorder.AudioSource.MIC。
3. Set output file format using MediaRecorder.setOutputFormat().
3. 使用MediaRecorder.setOutputFormat()设置输出文件格式。
4. Set output file name using MediaRecorder.setOutputFile().
4. 使用MediaRecorder.setOutputFile()设置输出文件名。
5. Set the audio encoder using MediaRecorder.setAudioEncoder().
5. 使用MediaRecorder.setAudioEncoder()设置音频编码器。
6. Call MediaRecorder.prepare() on the MediaRecorder instance.
6. 在MediaRecorder实例上调用MediaRecorder.prepare()。
7. To start audio capture, call MediaRecorder.start().
7. 为了开始音频捕捉,请调用MediaRecorder.start()。
8. To stop audio capture, call MediaRecorder.stop().
8. 为了停止音频捕捉,请调用MediaRecorder.stop()。
9. When you are done with the MediaRecorder instance, call MediaRecorder.release() on it. Calling MediaRecorder.release() is always recommended to free the resource immediately.
9. 当你使用完MediaRecorder实例时,请对它调用MediaRecorder.release()。调用MediaRecorder.release()总是建议的,以立即释放资源。
Example: Record audio and play the recorded audio
示例:记录音频和播放被记录的音频
The example class below illustrates how to set up, start and stop audio capture, and to play the recorded audio file.
下面的示例类描述如何配置、启动和停止音频捕捉,以及如何播放被记录的音频文件。
-------------------------------
/*
* The application needs to have the permission to write to external storage
* if the output file is written to the external storage, and also the
* permission to record audio. These permissions must be set in the
* application's AndroidManifest.xml file, with something like:
*
* 该应用程序需要拥有权限来写入外部存储如果输出文件被写入到外部存储,
* 还需要记录音频的权限。这些权限必须被设置在应用程序的AndroidManifest.xml
* 文件中,就像这样:
*
* <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
* <uses-permission android:name="android.permission.RECORD_AUDIO" />
*
*/
package com.android.audiorecordtest;
import android.app.Activity;
import android.widget.LinearLayout;
import android.os.Bundle;
import android.os.Environment;
import android.view.ViewGroup;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.content.Context;
import android.util.Log;
import android.media.MediaRecorder;
import android.media.MediaPlayer;
import java.io.IOException;
public class AudioRecordTest extends Activity
{
private static final String LOG_TAG = "AudioRecordTest";
private static String mFileName = null;
private RecordButton mRecordButton = null;
private MediaRecorder mRecorder = null;
private PlayButton mPlayButton = null;
private MediaPlayer mPlayer = null;
private void onRecord(boolean start) {
if (start) {
startRecording();
} else {
stopRecording();
}
}
private void onPlay(boolean start) {
if (start) {
startPlaying();
} else {
stopPlaying();
}
}
private void startPlaying() {
mPlayer = new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
mPlayer.prepare();
mPlayer.start();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
}
private void stopPlaying() {
mPlayer.release();
mPlayer = null;
}
private void startRecording() {
mRecorder = new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mRecorder.setOutputFile(mFileName);
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
mRecorder.prepare();
} catch (IOException e) {
Log.e(LOG_TAG, "prepare() failed");
}
mRecorder.start();
}
private void stopRecording() {
mRecorder.stop();
mRecorder.release();
mRecorder = null;
}
class RecordButton extends Button {
boolean mStartRecording = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onRecord(mStartRecording);
if (mStartRecording) {
setText("Stop recording");
} else {
setText("Start recording");
}
mStartRecording = !mStartRecording;
}
};
public RecordButton(Context ctx) {
super(ctx);
setText("Start recording");
setOnClickListener(clicker);
}
}
class PlayButton extends Button {
boolean mStartPlaying = true;
OnClickListener clicker = new OnClickListener() {
public void onClick(View v) {
onPlay(mStartPlaying);
if (mStartPlaying) {
setText("Stop playing");
} else {
setText("Start playing");
}
mStartPlaying = !mStartPlaying;
}
};
public PlayButton(Context ctx) {
super(ctx);
setText("Start playing");
setOnClickListener(clicker);
}
}
public AudioRecordTest() {
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
}
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
LinearLayout ll = new LinearLayout(this);
mRecordButton = new RecordButton(this);
ll.addView(mRecordButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
mPlayButton = new PlayButton(this);
ll.addView(mPlayButton,
new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
0));
setContentView(ll);
}
@Override
public void onPause() {
super.onPause();
if (mRecorder != null) {
mRecorder.release();
mRecorder = null;
}
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
}
-------------------------------
Except as noted, this content is licensed under Apache 2.0. For details and restrictions, see the Content License.
除特别说明外,本文在Apache 2.0下许可。细节和限制请参考内容许可证。
Android 4.0 r1 - 16 Apr 2012 17:06
-------------------------------
Portions of this page are modifications based on work created and shared by the Android Open Source Project and used according to terms described in the Creative Commons 2.5 Attribution License.
此页部分内容,是基于Android开源项目所创建和共享的工作,并且根据知识共享2.5署名许可证描述的条款来使用的修改版。
(本人翻译质量欠佳,请以官方最新内容为准,或者参考其它翻译版本:
* ソフトウェア技術ドキュメントを勝手に翻訳
http://www.techdoctranslator.com/android
* Ley's Blog
http://leybreeze.com/blog/
* 农民伯伯
http://www.cnblogs.com/over140/
* Android中文翻译组
http://androidbox.sinaapp.com/
)