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学习笔记>



posted @ 2021-06-26 23:16  北落不吉  阅读(265)  评论(0编辑  收藏  举报