1. 概要说明
Audio Queue 的播放流程即是一个生产者与消费者的模式,创建多个 QueueBuffers 容器,依次填充(生产)Buffer后插入队列中,开始播放(消费),然后通过回调将消费过的 Buffer 重置,循环整个过程.
2. 封装播放器
2.1 创建头文件,PCMPlayer.h
#import <Foundation/Foundation.h>
NS_ASSUME_NONNULL_BEGIN
@interface PCMPlayer : NSObject
- (void)playData:(uint8_t *)data withSize:(int)size;
@end
NS_ASSUME_NONNULL_END
2.2 实现头文件, PCMPlayer.m
#import "PCMPlayer.h"
#import <AudioToolbox/AudioToolbox.h>
#define QUEUE_BUFFER_SIZE 3 // 队列缓存个数
#define MIN_SIZE_PER_FRAME 1024 * 4 // 最小每帧大小
@interface PCMPlayer(){
AudioQueueRef audioQueue; // 音频播放队列
AudioStreamBasicDescription _audioDescription; // 音频流基本描述
AudioQueueBufferRef audioQueueB