Android音訊開發之MediaRecorder/MediaPlayer

總李寫程式碼發表於2016-05-10

前言:

上次我們介紹了實時採集音訊資料以及播放,今天我們來認識一下直接錄製檔案的方式。

 

直接上程式碼:錄製管理類

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

相關文章