Android实现直接播放麦克风采集到的声音
程序员文章站
2022-06-19 19:37:38
本文实例讲述了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程序设计有所帮助。
上一篇: 理解Objective-C的变量以及面相对象的继承特性
下一篇: 姜茶哪款好,你喝过哪款