当前位置:操作系统 > 安卓/Android >>

Android Audio代码分析2 - 函数getMinBufferSize

 

AudioTrack的使用示例中,用到了函数getMinBufferSize,今天把它倒出来,再嚼嚼。

 

 

*****************************************源码*************************************************

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

        int channelCount = 0;

        switch(channelConfig) {

        case AudioFormat.CHANNEL_OUT_MONO:

        case AudioFormat.CHANNEL_CONFIGURATION_MONO:

            channelCount = 1;

            break;

        case AudioFormat.CHANNEL_OUT_STEREO:

        case AudioFormat.CHANNEL_CONFIGURATION_STEREO:

            channelCount = 2;

            break;

        default:

            loge("getMinBufferSize(): Invalid channel configuration.");

            return AudioTrack.ERROR_BAD_VALUE;

        }

        

        if ((audioFormat != AudioFormat.ENCODING_PCM_16BIT)

            && (audioFormat != AudioFormat.ENCODING_PCM_8BIT)) {

            loge("getMinBufferSize(): Invalid audio format.");

            return AudioTrack.ERROR_BAD_VALUE;

        }

        

        if ( (sampleRateInHz < 4000) || (sampleRateInHz > 48000) ) {

            loge("getMinBufferSize(): " + sampleRateInHz +"Hz is not a supported sample rate.");

            return AudioTrack.ERROR_BAD_VALUE;

        }

       

        int size = native_get_min_buff_size(sampleRateInHz, channelCount, audioFormat);

        if ((size == -1) || (size == 0)) {

            loge("getMinBufferSize(): error querying hardware");

            return AudioTrack.ERROR;

        }

        else {

            return size;

        }

    } www.zzzyk.com

***********************************************************************************************

源码路径:

frameworks\base\media\java\android\media\AudioTrack.java

 

 

###########################################说明##############################################################

先把自带的注释拿来看看吧:

    /**

     * Returns the minimum buffer size required for the successful creation of an AudioTrack

     * object to be created in the {@link #MODE_STREAM} mode. Note that this size doesn't

     * guarantee a smooth playback under load, and higher values should be chosen according to

     * the expected frequency at which the buffer will be refilled with additional data to play.

     * @param sampleRateInHz the sample rate expressed in Hertz.

     * @param channelConfig describes the configuration of the audio channels.

     *   See {@link AudioFormat#CHANNEL_OUT_MONO} and

     *   {@link AudioFormat#CHANNEL_OUT_STEREO}

     * @param audioFormat the format in which the audio data is represented.

     *   See {@link AudioFormat#ENCODING_PCM_16BIT} and

     *   {@link AudioFormat#ENCODING_PCM_8BIT}

     * @return {@link #ERROR_BAD_VALUE} if an invalid parameter was passed,

     *   or {@link #ERROR} if the implementation was unable to query the hardware for its output

     *     properties,

     *   or the minimum buffer size expressed in bytes.

     */

从注释可以看出,通过该函数获取的最小buffer size,只是保证在MODE_STREAM模式下成功地创建一个AudioTrack对象。

并不能保证流畅地播放。

 

 

1、参数就不说了,可以参考上面注释,上一篇文章中也有说。

2、定义了一个内部变量:

     int channelCount = 0;

   用来记录声道数量。

   调用native函数native_get_min_buff_size时会用。

   可见buffer size也是由native层来决定的。

3、接下来根据Channel类型,计算声道数量:

        switch(channelConfig) {

        case AudioFormat.CHANNEL_OUT_MONO:

        case AudioFormat.CHANNEL_CONFIGURATION_MONO:

            channelCount = 1;

            break;

        case AudioFormat.CHANNEL_OUT_STEREO:

        case AudioFormat.CHANNEL_CONFIGURATION_STEREO:

            channelCount = 2;

            break;

        default:

            loge("getMinBufferSize(): Invalid channel configuration.");

            return AudioTrack.ERROR_BAD_VALUE;

        }

   MONO都是1,Stereo的都是2。

   不过,我们之前看过,Channel类型不止这几种。有以下一堆呢:

    public static final int CHANNEL_OUT_FRONT_LEFT = 0x4;

    public static final int CHANNEL_OUT_FRONT_RIGHT = 0x8;

    public static final int CHANNEL_OUT_FRONT_CENTER = 0x10;

    public static final int CHANNEL_OUT_LOW_FREQUENCY = 0x20;

    public static final int CHANNEL_OUT_BACK_LEFT = 0x40;

    public static final int CHANNEL_OUT_BACK_RIGHT = 0x80;

    public static final int CHANNEL_OUT_FRONT_LEFT_OF_CENTER = 0x100;

补充:移动开发 , Android ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,