当前位置: 代码迷 >> Android >> Android - MediaRecord
  详细解决方案

Android - MediaRecord

热度:82   发布时间:2016-04-27 23:29:34.0
Android -- MediaRecord

MediaRecord

集成了录音、编码、压缩等,支持少量的录音音频格式,大概有.aac .amr .3gp

优点:大部分以及集成,直接调用相关接口即可,代码量小

缺点:无法实时处理音频;输出的音频格式不是很多,例如没有输出mp3格式文件

WAV格式:录音质量高,但是压缩率小,文件大

AAC格式:相对于mp3,AAC格式的音质更佳,文件更小;有损压缩;一般苹果或者Android SDK4.1.2(API 16)及以上版本支持播放

AMR格式:压缩比比较大,但相对其他的压缩格式质量比较差,多用于人声,通话录音

至于常用的mp3格式,使用MediaRecorder没有该视频格式输出。一些人的做法是使用AudioRecord录音,然后编码成wav格式,再转换成mp3格式

举个栗子

 MediaRecorder recorder = new MediaRecorder();   recorder.setAudioSource(MediaRecorder.AudioSource.MIC);   recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);   recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);   recorder.setOutputFile(PATH_NAME);   recorder.prepare();   recorder.start();   // Recording is now started   //...   recorder.stop();   recorder.reset();   // You can reuse the object by going back to setAudioSource() step   recorder.release(); // Now the object cannot be reused  

Demo

    public static final String DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "1122" + File.separator;    private Button mStartBtn;    private Button mStopBtn;    private MediaRecorder mMediaRecorder;    private boolean mIsRecoding = false;	@Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_sound);        mStartBtn = (Button) findViewById(R.id.btn_start);        mStartBtn.setOnClickListener(this);        mStopBtn = (Button) findViewById(R.id.btn_stop);        mStopBtn.setOnClickListener(this);        initSound();    } 	private void initSound() {        mMediaRecorder = new MediaRecorder();        // 从麦克风源进行录音        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);        // 设置输出格式        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);        // 设置编码格式        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);    }	@Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn_start:                File dir = new File(DIR);                if (!dir.exists()) {                    dir.mkdir();                }                if (!mIsRecoding) {                    long time = System.currentTimeMillis();                    String fileName = time + ".amr";                    File file = new File(DIR + fileName);                    try {                        file.createNewFile();                    } catch (IOException e) {                        e.printStackTrace();                    }                    mMediaRecorder.setOutputFile(DIR + fileName);                    try {                        mMediaRecorder.prepare();                        mMediaRecorder.start();                    } catch (IOException e) {                        e.printStackTrace();                    }                    mIsRecoding = true;                }                break;            case R.id.btn_stop:                if (mIsRecoding) {                    mMediaRecorder.stop();                    mMediaRecorder.release();                    mIsRecoding = false;                }                break;        }    }

权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />      <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>      <uses-permission android:name="android.permission.RECORD_AUDIO" /> 

截图

我是天王盖地虎的分割线

  相关解决方案