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:
- Create a new instance of
android.media.MediaRecorder
. - Set the audio source using
MediaRecorder.setAudioSource()
. You will probably want to useMediaRecorder.AudioSource.MIC
. - Set output file format using
MediaRecorder.setOutputFormat()
. - Set output file name using
MediaRecorder.setOutputFile()
. - Set the audio encoder using
MediaRecorder.setAudioEncoder()
. - Call
MediaRecorder.prepare()
on the MediaRecorder instance. - To start audio capture, call
MediaRecorder.start()
. - To stop audio capture, call
MediaRecorder.stop()
. - When you are done with the MediaRecorder instance, call
MediaRecorder.release()
on it. CallingMediaRecorder.release()
is always recommended to free the resource immediately. //调用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:
* 需要的权限:
* <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;
}
}
}
Audio Capture 录音的更多相关文章
- H5调用手机的相机/摄像/录音等功能 _input:file的capture属性说明
H5使用input标签调用系统默许相机,摄像,录音功能.使用input:file标签, 去调用系统默认相机,摄像,录音功能,其实是有个capture属性,直接说明需要调用什么功能: <input ...
- 基于Allwinner的Audio子系统分析(Android-5.1)
前言 一直想总结下Audio子系统的博客,但是各种原因(主要还是自己懒>_<),一直拖到现在才开始重新整理,期间看过H8(Android-4.4),T3(Android-4.4),A64( ...
- html input 上capture 参数在 安卓 苹果上的异同
安卓上 <input type="file" accept="image/*" capture="camera"> //只调用相 ...
- 在webapp上使用input:file, 指定capture属性调用默许相机,摄像,录音功能
## 在webapp上使用input:file, 指定capture属性调用默认相机,摄像,录音功能 在iOS6下开发webapp,使用inputz之file,很有用 <input type=& ...
- Android Audio Play Out Channel
1: 7嘴8舌 扬声器, 耳机, 和听筒 就是通过: audiomanager.setmode(AudioManager.MODE_IN_COMMUNICATION)audiomanager.setS ...
- ios录音Demo
<AudioToolbox/AudioToolbox.h> :这个库是C的接口,偏向于底层,主要用于在线流媒体的播放 <AVFoundation/AVFoundation.h> ...
- 使用HTML5抓取 Audio & Video
原文地址: http://www.html5rocks.com/en/tutorials/getusermedia/intro/ 本地化的文章: http://www.html5rocks.com/z ...
- neo1973 audio subsystem
fhttp://wiki.openmoko.org/wiki/Neo_1973_audio_subsystem using Bluetooth headset with GSM NOTE none o ...
- 怎么调用html5的摄像头,录音,视频?
调用image 即打开相册或调用系统相机: <input type="file" accept="image/*" capture="camer ...
随机推荐
- nginx 编译选项
内容有些多,一眼看来难免头昏脑胀,但坚持看完,相信你一定会有所收获. nginx参数: --prefix= 指向安装目录 --sbin-path 指向(执行)程序文件(nginx) --conf-pa ...
- Python爬虫第一步
这只是记录一下自己学习爬虫的过程,可能少了些章法.我使用过的是Python3.x版本,IDE为Pycharm. 这里贴出代码集合,这一份代码也是以防自己以后忘记了什么,方便查阅. import req ...
- 谈谈react-router学习
源网页 http://qiutc.me/post/%E8%B0%88%E8%B0%88-react-router.html React + webpack 爽的飞起,少了点什么. 多页面,每次请求页面 ...
- [Forward]Use the SharePoint My Tasks Web Part outside of My Sites
from http://yalla.itgroove.net/2014/04/use-sharepoint-tasks-web-part-outside-sites/ Use the SharePoi ...
- unity3d 场景间数据传递
在游戏项目中,常常会使用到用户信息,获取信息当然可以从数据库中获取.但是对场景多的游戏这样做是不正确的,那么我我们就需要再第一次获取用户信息之后, 同时在其它的场景中共享用户数据,避免对服务器增加负担 ...
- Tautology
WFF 'N PROOF is a logic game played with dice. Each die has six faces representing some subset of th ...
- YII 主题
heming是一个在Web应用程序里定制网页外观的系统方式.通过采用一个新的主题,网页应用程序的整体外观可以立即和戏剧性的改变. 在Yii,每个主题由一个目录代表,包含view文件,layout文件和 ...
- Windows下Vundle插件BundleSearch命令出现错误解决方案
纯粹参考他人博文,已证明解决方案正确,转载时请注明参考链接!!! 参考链接: http://www.tuicool.com/articles/e2aiEn 在Windows下使用Vim时,用Vundl ...
- 揭开Linux操作系统的Swap交换区之谜
揭开Linux操作系统的Swap交换区之谜 Swap,即交换区,除了安装Linux的时候,有多少人关心过它呢?其实,Swap的调整对Linux服务器,特别是Web服务器的性能至关重要.通过调整Swap ...
- 构造函数语义学之Copy Constructor构建操作(2)
二.详述条件 3 和 4 那么好,我又要问大家了,条件1 和 2比较容易理解.因为member object或 base class 含有copy constructor.那么member objec ...