其他分享
首页 > 其他分享> > Android AudioTrack分析

Android AudioTrack分析

作者:互联网

1. 使用

1.1 音频参数

音频播放时有一些基本音频参数,包括[音频流类型、采样率、声道数、格式]

这些参数在Java层有定义,同时在system/media/audio/include/system/audio.h中有对应的定义

image

1.2 使用实例

    AudioTrack mAudioTrack;
    int mBufferSizeInBytes = AudioTrack.getMinBufferSize(SAMPLE_RATE, CHANNEL, AUDIO_FORMAT);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mAudioTrack = new AudioTrack.Builder()
                .setAudioAttributes(new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_MEDIA)
                        .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                        .setLegacyStreamType(AudioManager.STREAM_MUSIC)
                        .build())
                .setAudioFormat(new AudioFormat.Builder()
                        .setEncoding(AUDIO_FORMAT)
                        .setSampleRate(SAMPLE_RATE)
                        .setChannelMask(CHANNEL)
                        .build())
                .setTransferMode(AudioTrack.MODE_STREAM)
                .setBufferSizeInBytes(mBufferSizeInBytes)
                .build();
    } else {
        mAudioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, SAMPLE_RATE, CHANNEL, AUDIO_FORMAT,
                mBufferSizeInBytes, AudioTrack.MODE_STREAM);
    }
    
    mAudioTrack.play();
    mAudioTrack.write(bytes, 0, len);
    
    mAudioTrack.stop();
    mAudioTrack.release();

2. 流程

AudioTrack

参考:
<AudioTrack的Play流程>
<AudioTrack学习笔记>



标签:分析,STREAM,FORMAT,AudioTrack,音频,mAudioTrack,new,Android
来源: https://www.cnblogs.com/hzl6255/p/14939499.html