Android录音播放管理工具
程序员文章站
2023-12-21 11:10:58
1、语音播放直接用系统工具就好了,这个就不多说了,根据传入的路径(网络路径或本地路径均可)播放音频文件
/**
* created by zhb on 201...
1、语音播放直接用系统工具就好了,这个就不多说了,根据传入的路径(网络路径或本地路径均可)播放音频文件
/** * created by zhb on 2017/1/16. * 音乐在线播放 */ public class playmanager { private context mcontext; public playmanager(context context){ this.mcontext = context; } public void play(string song){ mediaplayer mp = new mediaplayer(); try { // 存储在sd卡或其他文件路径下的媒体文件 // 例如:mp.setdatasource("/sdcard/test.mp3"); // 网络上的媒体文件 // 例如:mp.setdatasource("http://www...../music/test.mp3"); mp.setdatasource(song); mp.prepare(); mp.start(); } catch (exception e) { e.printstacktrace(); } } }
2.录制amr格式音频文件(微信语音便用的这种格式,至于音频文件格式之间的比较请自行百度)
/** * created by zhb on 2017/1/16. * 本地录音 */ public class recordmanager { //录制成amr格式............................................................ private context mcontext; mediarecorder mediarecorder ; public recordmanager(context context){ this.mcontext = context; //todo 初始化安装路径,录音流程 } /**开始录制*/ public void start_amr(){ mediarecorder = new mediarecorder(); /** * mediarecorder.setaudiosource设置声音来源。 * mediarecorder.audiosource这个内部类详细的介绍了声音来源。 * 该类中有许多音频来源,不过最主要使用的还是手机上的麦克风,mediarecorder.audiosource.mic */ mediarecorder.setaudiosource(mediarecorder.audiosource.mic); /** * mediarecorder.setoutputformat代表输出文件的格式。该语句必须在setaudiosource之后,在prepare之前。 * outputformat内部类,定义了音频输出的格式,主要包含mpeg_4、three_gpp、raw_amr……等。 */ mediarecorder.setoutputformat(mediarecorder.outputformat.amr_nb); /** * mediarecorder.setaddioencoder()方法可以设置音频的编码 * audioencoder内部类详细定义了两种编码:audioencoder.default、audioencoder.amr_nb */ mediarecorder.setaudioencoder(mediarecorder.audioencoder.amr_nb); /** * 设置录音之后,保存音频文件的位置,一般是sd卡的位置 */ mediarecorder.setoutputfile(string.valueof(pathmanger.getvoicepath())); /** * 调用start开始录音之前,一定要调用prepare方法。 */ try { mediarecorder.prepare(); mediarecorder.start(); } catch (illegalstateexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } } /**停止录音*/ public void stop_amr(){ mediarecorder.stop(); mediarecorder.release(); mediarecorder = null; } /**重置录音*/ public void reset_amr(){ mediarecorder.reset(); } }
3、配置转换工具包(这个比较简单,配置以下文件即可)
添加flame.jar,并在armeabi和armeabi-v7a文件夹添加libmp3lame.so
资源文件:
4、录制mp3格式音频文件(个人觉得这种格式能比较好的统一android端和ios端的音频文件,虽然方法相对比较繁杂一些)
/** * created by zhb on 2017/1/16. * 本地录音 */ public class recordmanager { //录制成mp3格式.............................................. /**构造时候需要的activity,主要用于获取文件夹的路径*/ private activity activity; /**文件代号*/ public static final int raw = 0x00000001; public static final int mp3 = 0x00000002; /**文件路径*/ private string rawpath = null; private string mp3path = null; /**采样频率*/ private static final int sample_rate = 11025; /**录音需要的一些变量*/ private short[] mbuffer; private audiorecord mrecorder; /**录音状态*/ private boolean isrecording = false; /**是否转换ok*/ private boolean convertok = false; public recordmanager(activity activity, string rawpath, string mp3path) { this.activity = activity; this.rawpath = rawpath; this.mp3path = mp3path; } /**开始录音*/ public boolean start_mp3() { // 如果正在录音,则返回 if (isrecording) { return isrecording; } // 初始化 if (mrecorder == null) { initrecorder(); } getfilepath(); mrecorder.startrecording(); startbufferedwrite(new file(rawpath)); isrecording = true; return isrecording; } /**停止录音,并且转换文件,这很可能是个耗时操作,建议在后台中做*/ public boolean stop_mp3() { if (!isrecording) { return isrecording; } // 停止 mrecorder.stop(); isrecording = false; //todo // 开始转换 flameutils lameutils = new flameutils(1, sample_rate, 96); convertok = lameutils.raw2mp3(rawpath, mp3path); return isrecording ^ convertok;// convertok==true,return true } /**获取文件的路径*/ public string getfilepath(int filealias) { if (filealias == raw) { return rawpath; } else if (filealias == mp3) { return mp3path; } else return null; } /**清理文件*/ public void cleanfile(int cleanflag) { file f = null; try { switch (cleanflag) { case mp3: f = new file(mp3path); if (f.exists()) f.delete(); break; case raw: f = new file(rawpath); if (f.exists()) f.delete(); break; case raw | mp3: f = new file(rawpath); if (f.exists()) f.delete(); f = new file(mp3path); if (f.exists()) f.delete(); break; } f = null; } catch (exception e) { e.printstacktrace(); } } /**关闭,可以先调用cleanfile来清理文件*/ public void close() { if (mrecorder != null) mrecorder.release(); activity = null; } /**初始化*/ private void initrecorder() { int buffersize = audiorecord.getminbuffersize(sample_rate, audioformat.channel_in_mono, audioformat.encoding_pcm_16bit); mbuffer = new short[buffersize]; mrecorder = new audiorecord(mediarecorder.audiosource.mic, sample_rate, audioformat.channel_in_mono, audioformat.encoding_pcm_16bit, buffersize); } /**设置路径,第一个为raw文件,第二个为mp3文件*/ private void getfilepath() { try { string folder = "audio_recorder_2_mp3"; string filename = string.valueof(system.currenttimemillis()); if (rawpath == null) { file raw = new file(activity.getdir(folder, activity.mode_private), filename + ".raw"); raw.createnewfile(); rawpath = raw.getabsolutepath(); raw = null; } if (mp3path == null) { file mp3 = new file(activity.getdir(folder, activity.mode_private), filename + ".mp3"); mp3.createnewfile(); mp3path = mp3.getabsolutepath(); mp3 = null; } log.d("rawpath", rawpath); log.d("mp3path", mp3path); runcommand("chmod 777 " + rawpath); runcommand("chmod 777 " + mp3path); } catch (exception e) { e.printstacktrace(); } } /**执行cmd命令,并等待结果*/ private boolean runcommand(string command) { boolean ret = false; process process = null; try { process = runtime.getruntime().exec(command); process.waitfor(); ret = true; } catch (exception e) { e.printstacktrace(); } finally { try { process.destroy(); } catch (exception e) { e.printstacktrace(); } } return ret; } /**写入到raw文件*/ private void startbufferedwrite(final file file) { new thread(new runnable() { @override public void run() { dataoutputstream output = null; try { output = new dataoutputstream(new bufferedoutputstream( new fileoutputstream(file))); while (isrecording) { int readsize = mrecorder.read(mbuffer, 0, mbuffer.length); for (int i = 0; i < readsize; i++) { output.writeshort(mbuffer[i]); } } } catch (ioexception e) { e.printstacktrace(); } finally { if (output != null) { try { output.flush(); } catch (ioexception e) { e.printstacktrace(); } finally { try { output.close(); } catch (ioexception e) { e.printstacktrace(); } } } } } }).start(); } }
5、最后在自己想调用的地方调用就好了,pathmanger这个是我自己的路径管理工具,这里不贴了,反正自己直接放一个路径字符串进去就好了
/**初始化语音*/ private void initvoice() { //录音 recordmanager = new recordmanager( callhelpactivity.this, string.valueof(pathmanger.getvoicepathtoraw()), string.valueof(pathmanger.getvoicepathtomp3())); callhelp_voice_longclick.setontouchlistener(new view.ontouchlistener() { @override public boolean ontouch(view v, motionevent event) { switch(event.getaction()){ case motionevent.action_down: recordmanager.start_mp3(); break; case motionevent.action_move: break; case motionevent.action_up: recordmanager.stop_mp3(); break; } return false; } }); //语音播放 final playmanager playmanager = new playmanager(this); callhelp_voice_click.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { playmanager.play(string.valueof(pathmanger.getvoicepathtomp3())); } }); }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。