Android实现直接播放麦克风采集到的声音
程序员文章站
2023-11-08 14:26:22
本文实例讲述了android实现直接播放麦克风采集到的声音。分享给大家供大家参考。具体如下:
这是一个直接播放麦克风采集到的声音线程类:
class recor...
本文实例讲述了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程序设计有所帮助。