灵云人工智能论坛

 找回密码
 立即注册
搜索
查看: 4576|回复: 6

Android : 录音实现之AudioRecord类 [复制链接]

Rank: 10Rank: 10Rank: 10

最佳写手 活跃会员 突出贡献 精! 入门会员 回复小达人 邮箱勋章 手机 常驻居民 最佳写手(竖)

发表于 2014-12-12 09:25:08 |显示全部楼层

     Android系统提供了一些类以便应用去实现录音的功能。AndioRecord就是其中一个。那么我们如何通过AudioRecord去实现录音呢?笔者下面就给大家做一个介绍。

       首先看看Android帮助文档中对该类的简单概述: AndioRecord类的主要功能是让各种JAVA应用能够管理音频资源,以便它们通过此类能够录制平台的声音输入硬件所收集的声音。此功能的实现就是通过”pulling同步reading读取)AudioRecord对象的声音数据来完成的。在录音过程中,应用所需要做的就是通过后面三个类方法中的一个去及时地获取AudioRecord对象的录音数据. AudioRecord类提供的三个获取声音数据的方法分别是read(byte[], int, int), read(short[], int, int), read(ByteBuffer, int). 无论选择使用那一个方法都必须事先设定方便用户的声音数据的存储格式。



       开始录音的时候,一个AudioRecord需要初始化一个相关联的声音buffer, 这个buffer主要是用来保存新的声音数据。这个buffer的大小,我们可以在对象构造期间去指定。它表明一个AudioRecord对象还没有被读取(同步)声音数据前能录多长的音(即一次可以录制的声音容量)。声音数据从音频硬件中被读出,数据大小不超过整个录音数据的大小(可以分多次读出),即每次读取初始化buffer容量的数据。

       一般情况下录音实现的简单流程如下:

1.    创建一个数据流。

2.    构造一个AudioRecord对象,其中需要的最小录音缓存buffer大小可以通过getMinBufferSize方法得到。如果buffer容量过小,将导致对象构造的失败。

3.    初始化一个buffer,该buffer大于等于AudioRecord对象用于写声音数据的buffer大小。

4.    开始录音。

5.    从AudioRecord中读取声音数据到初始化buffer,将buffer中数据导入数据流。

6.    停止录音。

7.    关闭数据流。

程序示例 :

1.    JAVA

  1. public class myAudioRecorder extends Activity {
  2.     private boolean isRecording = false ;
  3.     private Object tmp = new Object() ;
  4.    
  5.    
  6.     @Override
  7.     public void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.main);
  10.       
  11.         Button start = (Button)findViewById(R.id.start_bt) ;
  12.         start.setOnClickListener(new OnClickListener()
  13.         {
  14.             @Override
  15.             public void onClick(View arg0) {
  16.                 // TODO Auto-generated method stub
  17.                 Thread thread = new Thread(new Runnable() {
  18.                     public void run() {
  19.                       record();
  20.                     }   
  21.                   });
  22.                   thread.start();
  23.                   findViewById(R.id.start_bt).setEnabled(false) ;
  24.                   findViewById(R.id.end_bt).setEnabled(true) ;
  25.             }
  26.          
  27.         }) ;
  28.       
  29.         Button play = (Button)findViewById(R.id.play_bt) ;
  30.         play.setOnClickListener(new OnClickListener(){
  31.             @Override
  32.             public void onClick(View v) {
  33.                 // TODO Auto-generated method stub
  34.                 play();
  35.             }
  36.          
  37.         }) ;
  38.       
  39.         Button stop = (Button)findViewById(R.id.end_bt) ;
  40.         stop.setEnabled(false) ;
  41.         stop.setOnClickListener(new OnClickListener(){
  42.             @Override
  43.             public void onClick(View v) {
  44.                 // TODO Auto-generated method stub
  45.                 isRecording = false ;
  46.                 findViewById(R.id.start_bt).setEnabled(true) ;
  47.                 findViewById(R.id.end_bt).setEnabled(false) ;
  48.             }
  49.          
  50.         }) ;
  51.       
  52.     }
  53.     public void play() {
  54.       // Get the file we want to playback.
  55.       File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/reverseme.pcm");
  56.       // Get the length of the audio stored in the file (16 bit so 2 bytes per short)
  57.       // and create a short array to store the recorded audio.
  58.       int musicLength = (int)(file.length()/2);
  59.       short[] music = new short[musicLength];
  60.       try {
  61.         // Create a DataInputStream to read the audio data back from the saved file.
  62.         InputStream is = new FileInputStream(file);
  63.         BufferedInputStream bis = new BufferedInputStream(is);
  64.         DataInputStream dis = new DataInputStream(bis);
  65.          
  66.         // Read the file into the music array.
  67.         int i = 0;
  68.         while (dis.available() > 0) {
  69.           music = dis.readShort();
  70.           i++;
  71.         }
  72.         // Close the input streams.
  73.         dis.close();   
  74.         // Create a new AudioTrack object using the same parameters as the AudioRecord
  75.         // object used to create the file.
  76.         AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
  77.                                                11025,
  78.                                                AudioFormat.CHANNEL_CONFIGURATION_MONO,
  79.                                                AudioFormat.ENCODING_PCM_16BIT,
  80.                                                musicLength*2,
  81.                                                AudioTrack.MODE_STREAM);
  82.        // Start playback
  83.         audioTrack.play();
  84.      
  85.         // Write the music buffer to the AudioTrack object
  86.         audioTrack.write(music, 0, musicLength);
  87.         audioTrack.stop() ;
  88.       } catch (Throwable t) {
  89.         Log.e("AudioTrack","Playback Failed");
  90.       }
  91.     }
  92.     public void record() {
  93.       int frequency = 11025;
  94.       int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
  95.       int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
  96.       File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/reverseme.pcm");
  97.       
  98.       // Delete any previous recording.
  99.       if (file.exists())
  100.         file.delete();
  101.       // Create the new file.
  102.       try {
  103.         file.createNewFile();
  104.       } catch (IOException e) {
  105.         throw new IllegalStateException("Failed to create " + file.toString());
  106.       }
  107.       
  108.       try {
  109.         // Create a DataOuputStream to write the audio data into the saved file.
  110.         OutputStream os = new FileOutputStream(file);
  111.         BufferedOutputStream bos = new BufferedOutputStream(os);
  112.         DataOutputStream dos = new DataOutputStream(bos);
  113.         
  114.         // Create a new AudioRecord object to record the audio.
  115.         int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration,  audioEncoding);
  116.         AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,
  117.                                                   frequency, channelConfiguration,
  118.                                                   audioEncoding, bufferSize);
  119.       
  120.         short[] buffer = new short[bufferSize];  
  121.         audioRecord.startRecording();
  122.         isRecording = true ;
  123.         while (isRecording) {
  124.           int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);
  125.           for (int i = 0; i < bufferReadResult; i++)
  126.             dos.writeShort(buffer);
  127.         }
  128.         audioRecord.stop();
  129.         dos.close();
  130.       
  131.       } catch (Throwable t) {
  132.         Log.e("AudioRecord","Recording Failed");
  133.       }
  134.     }
  135. }
复制代码

2.    XML布局 :

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     android:orientation="vertical"
  4.     android:layout_width="fill_parent"
  5.     android:layout_height="fill_parent"
  6.     >
  7.     <Button
  8.         android:id = "@+id/start_bt" android:text = "Record Start"
  9.         android:textColor = "#DC143C" android:textStyle = "bold"
  10.         android:layout_width = "wrap_content" android:layout_height = "wrap_content"/>
  11.     <Button
  12.         android:id = "@+id/end_bt" android:text = "Record_Stop"
  13.         android:textColor = "#00008B" android:textStyle = "bold"
  14.         android:layout_width = "wrap_content" android:layout_height = "wrap_content"/>
  15.       
  16.     <Button
  17.         android:id = "@+id/play_bt" android:text = "Play Record"
  18.         android:textStyle =  "bold"
  19.         android:layout_width = "wrap_content" android:layout_height = "wrap_content"/>
  20. </LinearLayout>
复制代码

3.    权限 :

  1. <uses-permission android:name="android.permission.RECORD_AUDIO"></uses-permission>
  2.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />   
复制代码

结束,再会!

踩过的脚印
回复

举报

Rank: 10Rank: 10Rank: 10

高级会员 最佳新人 活跃会员 热心会员 推广达人 宣传达人 灌水之王 突出贡献 论坛元老 卯兔 初级会员 爱心大使 QQ勋章 手机 微信 宣传大使

发表于 2014-12-12 13:52:09 |显示全部楼层

Rank: 5Rank: 5

最佳新人 活跃会员 热心会员 推广达人 宣传达人 灌水之王 突出贡献

发表于 2014-12-14 16:54:41 |显示全部楼层
高端
sinovoice VIP 会员  年费 VIP 会员 

Rank: 12Rank: 12Rank: 12

 成长值: 8288

特殊贡献 超级版主 最佳新人 活跃会员 热心会员 推广达人 宣传达人 灌水之王 突出贡献 优秀版主 荣誉管理 论坛元老 精!精!精! 终极会员 智多星 皇家勋章 人气之星 常驻居民 幸福使者

发表于 2014-12-15 14:38:04 |显示全部楼层

Rank: 10Rank: 10Rank: 10

最佳写手 活跃会员 突出贡献 精! 入门会员 回复小达人 邮箱勋章 手机 常驻居民 最佳写手(竖)

发表于 2014-12-16 12:42:05 |显示全部楼层
short[] buffer = new short[bufferSize];
看到上述代码中的120行,有这么一列,音频数据保存的是二进制数据,如果使用short数组会保存2个字节,保存数组时会有自动排序的,使用byte数组,只有一个字节,保存的时候则不会有排序的情况。
在开发者使用中,建议还是使用byte[] buffer=new byte[bufferSize];
匿名  发表于 2019-1-5 15:32:08




write my essay










Gilting of granny weatherall
my car essays
popular blog proofreading site for phd
comparing books and movies essay
professional analysis essay editing service au
popular masters essay proofreading for hire
my favourite subject social studies essay rubric examples
Word ProcessingEquipment or Concept
ts eliot the wasteland essays
conclusion computer virus essay
cheap homework ghostwriting for hire for school
pay to do custom critical essay on usa
red black d
Maximale Betrokkenheid Met Behoud Van Distantie Essay
essay on indian film industry
transition words for contrast essays
an analysis of the book the killing game by joy
cheap essay for college
samples of mba thesis topics
best presentation ghostwriters services for university
thesis title about fraternity gin
an overview of the short story the dead child by gabrielle roy
essay on how math is used in everyday life
beowulf resume samples
professional cv editor website
english research paper samples

dissertation writing service
expository essay topics
write essays for me
cheap essay writing service

good compare contrast topics essay
Diet essay
best speech ghostwriters services for phd
do you underline essay titles in mla
multiple choice quiz questions
formats for essays
human resource management reflective essay outline
essay on stefan sagmeister
a comparison of oliver twist and david copperfield in english literature
top dissertation writers websites online
preparing essay tests
it takes a whole village to raise a child essay
thesis topic ideas for communication
popular masters essay ghostwriters site for masters
organic produce essays
a debate on my education
popular essay editor site for university
professional best essay editor services for masters
short essay on no work is mean
cheap essay on algebra
Custom Essay Sales
a casual affair maugham essay
cover letter to hotel
admission editor service online
ben franklin: diest
essay about happiness free
Dress Codes and Expression Restraints
esl best essay writing websites us
well written descriptive essay examples
television norms
huqooq e niswan in urdu essay for kids
top blog post editor websites ca
custom argumentative essay proofreading websites for university
essay leadership reflection assignment nursing

ielts general training essay writing samples
esl course work writing site gb
esl bibliography ghostwriting services for school
Ethical Concerns of Genetics
an introduction to the life of hernan cortes
top biography ghostwriter websites ca
essay words transition
essay topics on pop culture
vinyl boronic acid synthesis essay
help me write education essays
匿名  发表于 6 天前


Link ----> AN ASPECT OF A RELATIONSHIP



essayerudite.com write my essay https://essayerudite.com/write-my-essay/










professional descriptive essay proofreading website for college
restate thesis definition
dissertation proposal outline template
dissertation chapter writers services
ancient egypt history essays
best research proposal ghostwriting websites for university
top homework writing service for school
Sililoqy of Kate
thesis theme background
professional research paper ghostwriter for hire
Children and crime
essay william blake
sample speeches
pro con essay on school uniforms
professional personal statement ghostwriting for hire for masters
professional creative essay editing site for university
popular dissertation methodology ghostwriters for hire for phd
top thesis statement writer sites uk
thesis + ortodontics
Global Warming: The Greenhouse Effect
esl university essay writing website for school
good term paper sample

do my homework
college essay help
write essays for me
argumentative essay topics

best topics for a research paper
cheap book review ghostwriter for hire online
doctor of ministry no dissertation
the pillow book essay topics rtr
Self - Directed Learning
professional school best essay help
top critical essay ghostwriter site uk
top thesis statement editing for hire us
bad eating habits essay
custom business plan writers sites for school
help write a research paper
research paper on how household chemicals can affect plants
thesis of reefer madness
hooks for essays list
buy professional academic essay on trump
parenting thesis
popular curriculum vitae writer site for masters
custom cv proofreading sites for masters
free romantic essays
online paper writing services
persuasive essay topics business
michelle obama princeton thesis snopes

rosetta stone research paper
thesis statement examples for evaluation essays
lord of the flies essay symbolism
federalist papers thesis
popular definition essay proofreading sites usa
验证问答 换一个 验证码 换一个

Archiver|任务|灵云 ( 京ICP证030095号 )

GMT+8, 2019-1-18 18:26 , Processed in 0.087059 second(s), 22 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部