Android音频开发之MediaRecorder/MediaPlayer
前言:
上次我们介绍了实时采集音频数据以及播放,今天我们来认识一下直接录制文件的方式。
直接上代码:录制管理类
public class MediaRecorderManager { public static final String TAG = "Recorder"; private MediaRecorder mRecorder; private static MediaRecorderManager mInstance; public MediaRecorderManager() { } /** * 获取单例引用 * * @return */ public static MediaRecorderManager getInstance() { if (mInstance == null) { synchronized (MediaRecorderManager.class) { if (mInstance == null) { mInstance = new MediaRecorderManager(); } } } return mInstance; } /** * 开始录制 * * @param filePath */ public void start(String filePath) { try { if(mRecorder==null) { mRecorder = new MediaRecorder(); mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);//设置音频采集方式 mRecorder.setOutputFormat(MediaRecorder.OutputFormat.AMR_NB);//设置音频输出格式 mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);//设置音频编码方式 } mRecorder.setOutputFile(filePath);//设置录音文件输出路径 mRecorder.prepare(); mRecorder.start(); } catch (Exception e) { } } /** * 释放录音资源 */ public void stop() { try { if (mRecorder != null) { mRecorder.stop(); mRecorder.release(); } } catch (IllegalStateException e) { } catch (RuntimeException e) { } catch (Exception e) { } mRecorder = null; } }
播放管理类:
public class MediaPlayerManager { private static MediaPlayerManager mInstance; private MediaPlayer player; /** * 获取单例引用 * * @return */ public static MediaPlayerManager getInstance() { if (mInstance == null) { synchronized (MediaPlayerManager.class) { if (mInstance == null) { mInstance = new MediaPlayerManager(); } } } return mInstance; } /** * 播放录音 * * @param url * @return */ public boolean play(String url) { return play(url, new OnCompletionListener() { @Override public void onCompletion(MediaPlayer arg0) { stop(); } }, new OnErrorListener() { @Override public boolean onError(MediaPlayer arg0, int arg1, int arg2) { stop(); return false; } }); } /** * 播放录音 * @param url * @return */ public boolean play(String url, OnCompletionListener completionListener, OnErrorListener errorListener) { stop(); if (TextUtils.isEmpty(url)) { return false; } try { if (player == null) { player = new MediaPlayer(); player.setDataSource(url); player.setVolume(0.7f, 0.7f); player.setAudioStreamType(AudioManager.STREAM_MUSIC); player.prepare(); player.start(); player.setOnCompletionListener(completionListener); player.setOnErrorListener(errorListener); return true; } } catch (Exception e) { stop(); } return false; } /** * 释放资源 */ public void stop() { if (player != null) { try { player.stop(); player.release(); } catch (Exception e) { e.printStackTrace(); } finally { player = null; } } } }
使用前要添加如下权限:
<!-- 录音权限 -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
相关音频知识链接:
http://ticktick.blog.51cto.com/823160/1748506
干我们这行,啥时候懈怠,就意味着长进的停止,长进的停止就意味着被淘汰,只能往前冲,直到凤凰涅槃的一天!