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 ...
随机推荐
- android:persistent属性
application PhoneApp既没有被Broadcast唤醒,也没有被其他service调用,那么是android是通过什么方式来启动PhoneApp,所以就发现了属性android:per ...
- iscc2016-好长的字符串
Vm0wd2QyVkhVWGhVYmxKV1YwZDRXRmxVUm5kVlJscHpXa2M1VjFKdGVGWlZNbmhQWVd4YWMxZHViRmROYWxaeVdWZDRZV014WkhG ...
- IOS设计模式之三:MVC模式
IOS设计模式之三:MVC模式 模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...
- Node.js Web模块
什么是Web服务器? Web服务器是处理由HTTP客户端发送的,如web浏览器的HTTP请求的软件应用程序,并返回响应于客户端网页. Web服务器通常伴随着图片,样式表和脚本的HTML文档. 大多数W ...
- About javascript language
Core http://www.cnblogs.com/leoo2sk/archive/2010/12/19/ecmascript-scope.html function(arguments) fun ...
- python operator模块
官网:https://docs.python.org/3.4/library/operator.html http://pymotw.com/2/operator/
- Sublime Text 3 配置
配置: { "font_face": "Source Code Pro", , "highlight_line": true, " ...
- 一键安装mysql5.6
#!/bin/bash # @Name:install_mysql.sh # @Author:Eivllom # @Create -- # @Modify -- app_soft="/app ...
- 数据结构(树链剖分,堆):HNOI 2016 network
2215. [HNOI2016]网络 ★★★☆ 输入文件:network_tenderRun.in 输出文件:network_tenderRun.out 简单对比时间限制:2 s 内存 ...
- 网络流(最大流) CQOI 2015 BZOJ 3931 网络吞吐量
3931: [CQOI2015]网络吞吐量 Description 路由是指通过计算机网络把信息从源地址传输到目的地址的活 动,也是计算机网络设计中的重点和难点.网络中实现路由转发的硬件设备称为路由器 ...