Android Audio上层实现

一. 从AudioTrack类说起

     AudioTrack适用于解码后的PCM流数据格式,参考AudioTrack类的最基本构造器的参数列表:

public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat, int bufferSizeInBytes, int mode)

 {

         ...  ...

  }

   streamType参数定义在AudioSystem类中, 由AudioManager类向app层返还调用

 frameworks/base/media/java/android/media

... ...

public static final int STREAM_DEFAULT = -1;                       //默认的audio流以及音量

public static final int STREAM_VOICE_CALL = 0;                  //通话中

public static final int STREAM_SYSTEM = 1;                         //系统提示音(低电量,锁屏)

public static final int STREAM_RING = 2;                              //来电铃声,短信声

public static final int STREAM_MUSIC = 3;                            //音乐播放声

public static final int STREAM_ALARM = 4;                           //警报声

public static final int STREAM_NOTIFICATION = 5;              //消息提示音

public static final int STREAM_BLUETOOTH_SCO = 6;            //通话中,连接上蓝牙

public static final int STREAM_SYSTEM_ENFORCED = 7;       //某些国家强制规定的系统提示音

public static final int STREAM_DTMF = 8;                                 //双向多频拨号盘,拨号声

public static final int STREAM_TTS = 9;                                   //麦克风输入声

public static final int STREAM_ACCESSIBILITY = 10;             //可扩展的其他声音

... ...

   sampleRateInHz,声音采样频率,介于4000-44100Hz即可。channelConfig参数定义在AudioFormat类中,如下

... ...

public static final int CHANNEL_CONFIGURATION_INVALID   = 0;                            //非法无效的channel配置
public static final int CHANNEL_CONFIGURATION_DEFAULT   = 1;                          //默认channel配置
public static final int CHANNEL_CONFIGURATION_MONO   = 2;                              //单声道channel配置
public static final int CHANNEL_CONFIGURATION_STEREO   = 3;                          //双声道channel配置

... ...

   audioFormat参数也在AudioFormat类中,不仅包含了PCM流格式,还包含了编码格式

... ...

public static final int ENCODING_PCM_16BIT = 2;              // 16bit/点进行采样
public static final int ENCODING_PCM_8BIT = 3;                //  8bit/点进行采样
public static final int ENCODING_PCM_FLOAT = 4;            // float/点进行采样
public static final int ENCODING_AC3 = 5;
public static final int ENCODING_E_AC3 = 6;
public static final int ENCODING_DTS = 7;
public static final int ENCODING_DTS_HD = 8;
public static final int ENCODING_MP3 = 9;
public static final int ENCODING_AAC_LC = 10;
public static final int ENCODING_AAC_HE_V1 = 11;
public static final int ENCODING_AAC_HE_V2 = 12;

... ...

    bufferSize参数定义在AudioTrack类中,对每次写入缓冲区的数据大小进行限制,并初步判定sampleRateInHz, channelConfig以及audioFormat输入是否合理:

... ...

static public int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat){ ... }

... ...

    mode参数定义在AudioSystem类中,设定手机所处状态

... ...   

public static final int MODE_INVALID  = -2;                               //非法无效模式配置
public static final int MODE_CURRENT = -1;                              //当前默认模式
public static final int MODE_NORMAL  = 0;                                //正常模式
public static final int MODE_RINGTONE  = 1;                            //耳机模式
public static final int MODE_IN_CALL = 2;                                 //来电模式

public static final int MODE_IN_COMMUNICATION   = 3;         //通话模式
public static final int NUM_MODES = 4;

... ...

二.  AudioTrack 样例

    我们以双声道,8000Hz采样频率,播放音乐作为预设条件

int mBufferSize = AudioTrack.getMinBufferSize( );

AudioTrack mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,

                                                           8000, AudioFormat.CHANNEL_CONFIGURATION_STEREO,

                                                           AudioFormat.ENCODING_PCM_16BIT, mBufferSize, AudioSystem.MODE_NORMAL);

mAudioTrack.play();

byte[] audiodata = new byte[100];

... ...                                                                                             //初始化audiodata

mAudioTrack.write(audiodata, 0, audiodata.length)                   //取决于要写入多少audiodata

mAudioTrack.stop();

mAudioTrack.release();                                                          //释放AudioTrack资源,否则不容易被GC回收

 

你可能感兴趣的:(Android)