(网络共享)audiorecord实现录音

上传人:wm****3 文档编号:42887559 上传时间:2018-06-04 格式:DOC 页数:5 大小:44.50KB
返回 下载 相关 举报
(网络共享)audiorecord实现录音_第1页
第1页 / 共5页
(网络共享)audiorecord实现录音_第2页
第2页 / 共5页
(网络共享)audiorecord实现录音_第3页
第3页 / 共5页
(网络共享)audiorecord实现录音_第4页
第4页 / 共5页
(网络共享)audiorecord实现录音_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《(网络共享)audiorecord实现录音》由会员分享,可在线阅读,更多相关《(网络共享)audiorecord实现录音(5页珍藏版)》请在金锄头文库上搜索。

1、AudioRecord 实现录音实现录音分类: 音频处理 Android2011-06-09 13:491369人阅读评论(0)收藏举报Raw Audio Manipulation in AndroidTuesday, September 1, 2009 | 2:13 AM Labels: Country: EMEA, Product: Android Version 1.5of the Android SDK introduced a bunch of cool new features for developers. Though not as glamorous as some APIs

2、, the new audio manipulation classes - AudioTrack and AudioRecord - offer powerful functionality to developers looking to manipulate raw audio.These classes let you record audio directly from the audio input hardware of the device, and stream PCM audio buffers to the audio hardware for playback. Str

3、ong sauce indeed for those of you looking to have more control over audio input and playback.Enough talk - on to the code. To test out these new APIs I put together a simple Android app that listens to 10 seconds of input from the microphone and then plays it back through the speaker in reverse. Per

4、fect for decoding secret messages in old Beatles albums.Start with the recording code. Its designed to record the incoming audio to a file on the SD Card that well read and playback later. As per the latest security patch, your application requires a uses-permission to record audio.The recording cod

5、e here records a new set of 16bit mono audio at 11025Hz to reverseme.pcm on the SD card.public void record() int frequency = 11025;int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;File file = new File(Environment.getExternalStorageD

6、irectory().getAbsolutePath() + “/reverseme.pcm“);/ Delete any previous recording.if (file.exists()file.delete();/ Create the new file.try file.createNewFile(); catch (IOException e) throw new IllegalStateException(“Failed to create “ + file.toString();try / Create a DataOuputStream to write the audi

7、o data into the saved file.OutputStream os = new FileOutputStream(file);BufferedOutputStream bos = new BufferedOutputStream(os);DataOutputStream dos = new DataOutputStream(bos);/ Create a new AudioRecord object to record the audio.int bufferSize = AudioRecord.getMinBufferSize(frequency, channelConfi

8、guration, audioEncoding);AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,frequency, channelConfiguration,audioEncoding, bufferSize);short buffer = new shortbufferSize;audioRecord.startRecording();while (isRecording) int bufferReadResult = audioRecord.read(buffer, 0, bufferSiz

9、e);for (int i = 0; i 0) musicmusicLength-1-i = dis.readShort();i+;/ Close the input streams.dis.close();/ Create a new AudioTrack object using the same parameters as the AudioRecord/ object used to create the file.AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,11025,AudioFormat.CHA

10、NNEL_CONFIGURATION_MONO,AudioFormat.ENCODING_PCM_16BIT,musicLength,AudioTrack.MODE_STREAM);/ Start playbackaudioTrack.play();/ Write the music buffer to the AudioTrack objectaudioTrack.write(music, 0, musicLength); catch (Throwable t) Log.e(“AudioTrack“,“Playback Failed“); Finally, to drive this you

11、 need to update your application Activity to call the record and playback methods as appropriate. To keep this example as simple as possible Im going to record for 10 seconds as soon as the application starts, and playback in reverse as soon as Ive finished taking the sample.To be more useful youd a

12、lmost certainly want to perform the playback operation in a Service and on a background thread.Override public void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.main);Thread thread = new Thread(new Runnable() public void run() record(););thread.start

13、();try wait(10000); catch (InterruptedException e) isRecording = false;try thread.join(); catch (InterruptedException e) play();finish(); The AudioTrack and AudioRecord classes offer a lot more functionality than Ive demonstrated here. Using the AudioTrack streaming mode you can do processing of incoming audio and playback in near real time, letting you manipulate incoming or outgoing audio and perform signal processing on raw audio on the device.Tell us how youve used these APIs in your Android apps in the comments!

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 生活休闲 > 社会民生

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号