Android音频开发之音频采集(MediaRecord)

版权声明:本文为卫伟学习总结文章,转载请注明出处!
AudioRecord和MediaRecorder两种都可以录制音频,MediaRecorder已实现大量的封装,操作起来更加简单,而AudioRecord使用起来更加灵活,能实现更多的功能。
1.AudioRecord(基于字节流录音)
优点:可以实现语音的实时处理,进行边录边播,对音频的实时处理。
缺点:输出是PCM的语言数据,如果保存成音频文件是不能被播放器播放的。要用到AudioTrack这个去进行处理。
2.MediaRecorder(基于文件录音)
已集成了录音,编码,压缩等,支持少量的音频格式文件。
优点:封装度很高,操作简单
缺点:无法实现实时处理音频,输出的音频格式少。
MediaRecorder基于API介绍
MediaRecorder是android中面向应用层的封装,用于提供便捷的音视频编码封装操作,在使用的过程中要严格按照官方指定的生命周期调用顺序,即下图所示的使用步骤

Android音频开发之音频采集(MediaRecord)_第1张图片
从图中可以看出,MediaRecorder的生命周期有一下几个阶段,并且必须按顺序执行。

  • initial:在MediaRecorder被创建(刚new出来)或者调用reset()方法后,会处于该状态
  • initialized : 当调用setAudioSource()或者setVideoSource()后,处于该状态。这两个方法主要用于设置音视频的源配置,通常音频是麦克风,视频是摄像头。该状态可以通过调用reset()方法回到initial状态
  • DataSourceConfigured : 当调用setOutputFormat()方法后,会处于该状态。该方法主要用于设置输出的文件格式,可以是音视频如MP4,也可以是单独的音频如mp3。当处于该状态之后,可以进一步设置音频和视频的配置参数,例如音频封装格式,采样率,视频码率,帧率等等该状态可以通过调用reset()方法回到initial状态
  • Prepared :在上面几个步骤都配置好之后,可以通过调用prepare()方法进入该状态,只有处于该状态,才能调用start()
  • Recording :通过调用start()方法进入该状态,该状态就是真正开始进行视频录制编码的阶段,通过调用stop()或者reset()可以回到initial状态
  • error状态 :当录制过程中发生次错误时,会进入该状态,调用reset()方法回到initial状态。
  • release : 只有在initial状态才可以通过调用release()方法进入该状态,释放所占用的系统资源

功能实现:MediaRecorder(这里需要注意,无论录制还是播放都时一个好耗时操作,需要在非主线程中去操作)

private void initView() {
    tv_sendmsg = (TextView) findViewById(R.id.tv_stream_msg);
    tv_press_send = (TextView) findViewById(R.id.tv_press_send);
    //实现触摸录音,松开录音结束     
    tv_press_send.setOnTouchListener(new View.OnTouchListener() {
        
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startRecorder();
                break;
            case MotionEvent.ACTION_UP:
                stopRecorder();
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            default:
                break;
            }
            return true;
        }
    });
}

开启一个单线程去实现录音功能,及失败操作:

/**
 * 开启录音
 */
private void startRecorder() {
    tv_press_send.setText("正在说话...");
    //提交后台任务,开始录音
    mExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            //释放上一次的录音
            releaseRecorder();
            Log.i("wewei","开始录音");
            //开始录音
            if(!doStart()) {
                recorderFial();
            }
        }
    });
}
启动录音,及MediaRecorder的配置:

/**
 * 启动录音
 * @return //录音是否启动成功
 */
private boolean doStart() {
    try {
        Log.i("wewei","启动录音");
        //创建MediaRecorder
        mMediaRecorder = new MediaRecorder();
        // 创建录音文件
        mRecorderFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/recorderdemo/" +System.currentTimeMillis() + ".m4a");
         if (!mRecorderFile.getParentFile().exists()) mRecorderFile.getParentFile().mkdirs();
            mRecorderFile.createNewFile();
         //配置MediaRecorder
         
         // 从麦克风采集
         mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
         
         // 保存文件为MP4格式
         mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
         
         // 所有android系统都支持的适中采样的频率
         mMediaRecorder.setAudioSamplingRate(44100);
         
         //通用的AAC编码格式
         mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
         
         //设置音质频率
         mMediaRecorder.setAudioEncodingBitRate(96000);
         
         //设置文件录音的位置
         mMediaRecorder.setOutputFile(mRecorderFile.getAbsolutePath());
         
         //开始录音
         mMediaRecorder.prepare();
         mMediaRecorder.start();
         startRecorderTime = System.currentTimeMillis();
         Log.i("wewei","正在录音");
         
    } catch(Exception e) {
        Toast.makeText(MainActivity.this, "录音失败,请重试", Toast.LENGTH_SHORT).show();
        return false;
    }
    
    //记录开始录音时间,用于统计时长,小于3秒中,录音不发送
    return true;
}

停止录音操作:

 /**
 * 关闭录音
 * 
 * @return
 */
private boolean doStop() {
    try {
        Log.i("wewei","关闭录音");
        mMediaRecorder.stop();
        stopRecorderTime = System.currentTimeMillis();
        final int second = (int) (stopRecorderTime - startRecorderTime) / 1000;
        //按住时间小于3秒钟,算作录取失败,不进行发送s
        if(second < 3) return false;
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                //tv_sendmsg.setText("录制成功:" + second + "秒");
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}

释放MediaRecorder:

/**
 * 释放上一次的录音
 */
private void releaseRecorder() {
    if (mMediaRecorder != null) {
        mMediaRecorder.release();
        mMediaRecorder = null;
    }
}

录音的播放:使用MediaPlayer
开启一个单线程,去播放:

/**
 * 播放录音
 *
 * @param view
 */
public void playrecorder(View view) {
    if (!mIsPlaying) {
        mExecutorService.submit(new Runnable() {
            @Override
            public void run() {
                doPlay(mRecorderFile);
            }
        });

    } else {
        Toast.makeText(FileActivity.this, "正在播放", Toast.LENGTH_SHORT).show();
    }
}

具体播放代码:

/**
 * 播放
 * @param audioFile
 */
private void doPlay(File audioFile) {
    Log.i("wewei","doPlay");
    Log.i("wewei","audioFile ====" +audioFile);
    try {
        //配置播放器mMediaPlayer
        mMediaPlayer = new MediaPlayer();
        FileInputStream fis = new FileInputStream(audioFile);       
        //设置声音文件
        mMediaPlayer.setDataSource(fis.getFD());
        //配置音量,中等音量
        mMediaPlayer.setVolume(1, 1);
        //播放是否循环
        mMediaPlayer.setLooping(false);
        
        //设置监听回调, 播放完毕
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            
            @Override
            public void onCompletion(MediaPlayer mp) {
                stopPlayer();       
                Log.i("wewei","播放完毕");
            }
        });
        
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                stopPlayer();
                Toast.makeText(MainActivity.this,"播放失败",Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        Log.i("wewei","prepare");
        //设置播放
        mMediaPlayer.prepare();
        mMediaPlayer.start();
        Log.i("wewei","正在播放");
        
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("wewei","error :" +e.toString());
        stopPlayer();
    }
}

activity销毁后的onDestory的处理:

@Override
protected void onDestroy() {
    super.onDestroy();
    //当activity关闭时,停止这个线程,防止内存泄漏
    mExecutorService.shutdownNow();
    releaseRecorder();
}

MediaRecord完整功能实现代码:

package com.weiwei.mediarecordertest;

import java.io.File;
import java.io.FileInputStream;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
 * MediaRecorder 实现录音采集
 * MediaPlayer 实现录音播放
 * @author weiwei
 * 
 */
public class MainActivity extends Activity {
private TextView tv_sendmsg;
private TextView tv_press_send;
private ExecutorService mExecutorService;
private MediaRecorder mMediaRecorder;
private MediaPlayer mMediaPlayer;
private File mRecorderFile;
private long startRecorderTime, stopRecorderTime;
private Handler mHandler = new Handler(Looper.getMainLooper());
private boolean mIsPlaying = false;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.send_file);
    setTitle("文件录音");
    initView();
    //录音JNI函数不具有线程安全型,因此用单线程
    mExecutorService = Executors.newSingleThreadExecutor();
}

private void initView() {
    tv_sendmsg = (TextView) findViewById(R.id.tv_stream_msg);
    tv_press_send = (TextView) findViewById(R.id.tv_press_send);
    tv_press_send.setOnTouchListener(new View.OnTouchListener() {
        
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                startRecorder();
                break;
            case MotionEvent.ACTION_UP:
                stopRecorder();
                break;
            case MotionEvent.ACTION_CANCEL:
                break;
            default:
                break;
            }
            return true;
        }
    });
}

/**
 * 开启录音
 */
private void startRecorder() {
    tv_press_send.setText("正在说话...");
    //提交后台任务,开始录音
    mExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            //释放上一次的录音
            releaseRecorder();
            Log.i("wewei","开始录音");
            //开始录音
            if(!doStart()) {
                recorderFial();
            }
        }
    });
}

/**
 * 释放上一次的录音
 */
private void releaseRecorder() {
    if (mMediaRecorder != null) {
        mMediaRecorder.release();
        mMediaRecorder = null;
    }
}

/**
 * 启动录音
 * @return //录音是否启动成功
 */
private boolean doStart() {
    try {
        Log.i("wewei","启动录音");
        //创建MediaRecorder
        mMediaRecorder = new MediaRecorder();
        // 创建录音文件
        mRecorderFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
                + "/recorderdemo/" +System.currentTimeMillis() + ".m4a");
         if (!mRecorderFile.getParentFile().exists()) mRecorderFile.getParentFile().mkdirs();
            mRecorderFile.createNewFile();
         //配置MediaRecorder
         
         // 从麦克风采集
         mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
         
         // 保存文件为MP4格式
         mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
         
         // 所有android系统都支持的适中采样的频率
         mMediaRecorder.setAudioSamplingRate(44100);
         
         //通用的AAC编码格式
         mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
         
         //设置音质频率
         mMediaRecorder.setAudioEncodingBitRate(96000);
         
         //设置文件录音的位置
         mMediaRecorder.setOutputFile(mRecorderFile.getAbsolutePath());
         
         //开始录音
         mMediaRecorder.prepare();
         mMediaRecorder.start();
         startRecorderTime = System.currentTimeMillis();
         Log.i("wewei","正在录音");
         
    } catch(Exception e) {
        Toast.makeText(MainActivity.this, "录音失败,请重试", Toast.LENGTH_SHORT).show();
        return false;
    }
    
    //记录开始录音时间,用于统计时长,小于3秒中,录音不发送
    return true;
}

/**
 * 关闭录音
 * 
 * @return
 */
private boolean doStop() {
    try {
        Log.i("wewei","关闭录音");
        mMediaRecorder.stop();
        stopRecorderTime = System.currentTimeMillis();
        final int second = (int) (stopRecorderTime - startRecorderTime) / 1000;
        //按住时间小于3秒钟,算作录取失败,不进行发送s
        if(second < 3) return false;
        mHandler.post(new Runnable() {

            @Override
            public void run() {
                //tv_sendmsg.setText("录制成功:" + second + "秒");
            }
        });
    } catch (Exception e) {
        e.printStackTrace();
    }
    return true;
}

/**
 * 录音失败逻辑
 */
private void recorderFial() {
    mRecorderFile = null;
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            tv_press_send.setText("录音失败请重新录音");
        }
    });
}

/**
 * 停止录音
 */
private void stopRecorder() {
    tv_press_send.setText("停止录音");
    //提交后台任务,停止录音
    mExecutorService.submit(new Runnable() {

        @Override
        public void run() {
            if(!doStop()) {
                recorderFial();
            }
            releaseRecorder();
        }
    });
}

/**
 * 播放录音
 * 
 * @param view
 */
public void playrecorder(View view) {
    Log.i("wewei","mIsPlaying =" +mIsPlaying);
    if(!mIsPlaying) {
        mExecutorService.submit(new Runnable() {

            @Override
            public void run() {
                doPlay(mRecorderFile);
            }
        });
    } else {
        Toast.makeText(MainActivity.this, "正在播放", Toast.LENGTH_SHORT).show();
    }
}

/**
 * 播放
 * @param audioFile
 */
private void doPlay(File audioFile) {
    Log.i("wewei","doPlay");
    Log.i("wewei","audioFile ====" +audioFile);
    try {
        //配置播放器mMediaPlayer
        mMediaPlayer = new MediaPlayer();
        FileInputStream fis = new FileInputStream(audioFile);       
        //设置声音文件
        mMediaPlayer.setDataSource(fis.getFD());
        //配置音量,中等音量
        mMediaPlayer.setVolume(1, 1);
        //播放是否循环
        mMediaPlayer.setLooping(false);
        
        //设置监听回调, 播放完毕
        mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            
            @Override
            public void onCompletion(MediaPlayer mp) {
                stopPlayer();       
                Log.i("wewei","播放完毕");
            }
        });
        
        mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                stopPlayer();
                Toast.makeText(MainActivity.this,"播放失败",Toast.LENGTH_SHORT).show();
                return true;
            }
        });
        Log.i("wewei","prepare");
        //设置播放
        mMediaPlayer.prepare();
        mMediaPlayer.start();
        Log.i("wewei","正在播放");
        
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("wewei","error :" +e.toString());
        stopPlayer();
    }
}

private void stopPlayer() {
    Log.i("wewei","stopPlayer");
    mIsPlaying = false;
    mMediaPlayer.release();
    mMediaPlayer = null;
}

@Override
protected void onDestroy() {
    super.onDestroy();
    //当activity 关闭时,停止这个线程,防止内存泄漏
    mExecutorService.shutdownNow();
    releaseRecorder();
}

生成的.m4a格式的音频文件
Android音频开发之音频采集(MediaRecord)_第2张图片

你可能感兴趣的:(Android音频开发之音频采集(MediaRecord))