音频播放时有一些基本音频参数,包括[音频流类型、采样率、声道数、格式]
这些参数在Java层有定义,同时在system/media/audio/include/system/audio.h中有对应的定义
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();
参考:
<AudioTrack的Play流程>
<AudioTrack学习笔记>