Android播放麦克风采集到的声音 Android实现直接播放麦克风采集到的声音
RobinTang 人气:0想了解Android实现直接播放麦克风采集到的声音的相关内容吗,RobinTang在本文为您仔细讲解Android播放麦克风采集到的声音的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,播放,声音,下面大家一起来学习吧。
本文实例讲述了Android实现直接播放麦克风采集到的声音。分享给大家供大家参考。具体如下:
这是一个直接播放麦克风采集到的声音线程类:
class RecordThread extends Thread{ static final int frequency = 44100; static final int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO; static final int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; @Override public void run() { // TODO Auto-generated method stub int recBufSize = AudioRecord.getMinBufferSize(frequency, channelConfiguration, audioEncoding)*2; int plyBufSize = AudioTrack.getMinBufferSize(frequency, channelConfiguration, audioEncoding)*2; AudioRecord audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC, frequency, channelConfiguration, audioEncoding, recBufSize); AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, frequency, channelConfiguration, audioEncoding, plyBufSize, AudioTrack.MODE_STREAM); byte[] recBuf = new byte[recBufSize]; audioRecord.startRecording(); audioTrack.play(); while(true){ int readLen = audioRecord.read(recBuf, 0, recBufSize); audioTrack.write(recBuf, 0, readLen); } audioTrack.stop(); audioRecord.stop(); } }
使用的时候,这样就行:
复制代码 代码如下:
RecordThread rec = new RecordThread();
rec.start();
rec.start();
需要在AndroidManifest.xml文件中添加路由权限:
复制代码 代码如下:
<uses-permission android:name="android.permission.RECORD_AUDIO"/>
希望本文所述对大家的Android程序设计有所帮助。
加载全部内容