Я хочу использовать AVAudioEngine для воспроизведения данных из сети, но когда я получаю данные из сети и использую scheduleBuffer для добавления данных, будет шум, или звук просто прервется
//init
- (void)viewDidLoad {
[super viewDidLoad];
[self initWebSocket:server_ip];
self.engine = [[AVAudioEngine alloc] init];
self.playerNode = [[AVAudioPlayerNode alloc] init];
[self.engine attachNode:self.playerNode];
self.format = [[AVAudioFormat alloc] initWithCommonFormat:AVAudioPCMFormatFloat32
sampleRate:(double)48000.0
channels:(AVAudioChannelCount)1
interleaved:NO];
AVAudioMixerNode *mainMixer = [self.engine mainMixerNode];
[self.engine connect:self.playerNode to:mainMixer format:self.format];
if (!self.engine.isRunning) {
[self.engine prepare];
NSError *error;
BOOL success;
success = [self.engine startAndReturnError:&error];
NSAssert(success, @"couldn't start engine, %@", [error localizedDescription]);
}
[self.playerNode play];
}
// get data from network
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message{
NSData *data = message;
AVAudioPCMBuffer *pcmBuffer = [[AVAudioPCMBuffer alloc]
initWithPCMFormat:self.format
frameCapacity:(uint32_t)(data.length)
/self.format.streamDescription->mBytesPerFrame];
pcmBuffer.frameLength = pcmBuffer.frameCapacity;
[data getBytes:*pcmBuffer.floatChannelData length:data.length];
[self.playerNode scheduleBuffer:pcmBuffer completionHandler:nil];
}