android音频录音,(一)MediaRecorder简介

1.MediaRecorder概述

Android 多媒体框架支持捕获和编码各种常见的音频和视频格式,简要介绍音频录音。

2.MediaRecorder

源码路径:
frameworks/base/media/java/android/media/MediaRecorder.java

源码接口:

setAudioSource(MediaRecorder.AudioSource.MIC) 用于设置音频源,如:MediaRecorder.AudioSource.MIC,此MIC一般为设备主MIC:
源码函数:
public native void setAudioSource(@Source int audioSource)throws IllegalStateException;

setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)设置输出文件格式,如:MediaRecorder.OutputFormat.THREE_GPP,THREE_GPP表示录音保存文件为3gp:
源码函数:
public native void setOutputFormat(int output_format)throws IllegalStateException;

setOutputFile()设置输出文件名,如" sdcard/audiorecordtest.3gp":
源码函数:
public void setOutputFile(File file)
     {
          mPath = null;
          mFd = null;
          mFile = file;
     }

setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)设置音频编码器,MediaRecorder.AudioEncoder.AMR_NB,AMR_NB为编码格式:
源码函数:
public native void setAudioEncoder(int audio_encoder)throws IllegalStateException;

prepare()各参数设置完成后,启动初始化:
源码函数:
public void prepare() throws IllegalStateException, IOException
{
  if (mPath != null) {
      RandomAccessFile file = new RandomAccessFile(mPath, "rw");
      try {
          _setOutputFile(file.getFD());
      } finally {
          file.close();
      }
  } else if (mFd != null) {
      _setOutputFile(mFd);
  } else if (mFile != null) {
      RandomAccessFile file = new RandomAccessFile(mFile, "rw");
      try {
          _setOutputFile(file.getFD());
      } finally {
          file.close();
      }
  } else {
      throw new IOException("No valid output file");
  }
  _prepare();
}

start() 执行录音启动:
源码函数:
public native void start() throws IllegalStateException;

stop()停止录音:
源码函数:
public native void stop() throws IllegalStateException;

3 权限申请:

使用录音功能,APK必须告知用户它将访问设备的音频输入。从 Android 6.0(API 级别 23)开始,APK录音运行时,必须请求用户批准。APK 需要请求如下权限。
<uses-permission android:name="android.permission.RECORD_AUDIO" />

以上,MediaRecorder录取模式简要说明,可以参照源码进行查看。

不同参数类型: https://mp.csdn.net/mp_blog/creation/editor/140903503

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值