Audio Queue 如何停止当前播放任务并立即开始播放另一个 AudioQueueBuffer?
How Audio Queue stop current playing task and start play another AudioQueueBuffer immediately?
需要:我有一个音频队列和两个 AudioQueueBuffer
。我怎么玩
NO.2 AudioQueueBuffer
立即在 NO.1 AudioQueueBuffer
比赛中。
我试过 AudioQueueStop
或 AudioQueueReset
。处理时间很长,NO.2AudioQueueBuffer
玩的太晚了。
-(void)playBuffer:(AudioBuffer *)buffer format:(const AudioStreamBasicDescription *)format
{
AudioQueueStop(_audioQueue, YES);//this line consuming too much time
AudioQueueDispose(_audioQueue, YES);
AudioQueueRef newAudioQueue;
AudioQueueBufferRef queueBuffer;
AudioQueueNewOutput(format, audioQueueOutputCallback, (__bridge void*)self,
nil, nil, 0, &newAudioQueue);
OSStatus status;
status = AudioQueueAllocateBuffer(newAudioQueue, buffer->mDataByteSize, &queueBuffer);
memcpy(queueBuffer->mAudioData, buffer->mData, buffer->mDataByteSize);
queueBuffer->mAudioDataByteSize=buffer->mDataByteSize;
status = AudioQueueEnqueueBuffer(newAudioQueue, queueBuffer, 0, NULL);
Float32 gain=1.0;
AudioQueueSetParameter(newAudioQueue, kAudioQueueParam_Volume, gain);
AudioQueueStart(newAudioQueue, nil);
AudioQueueFreeBuffer(newAudioQueue, queueBuffer);
_audioQueue = newAudioQueue;
}
所以我的问题 is:Is 可以 audio queue
立即播放下一个音频缓冲区吗?或者 audio queue
不匹配此任务我需要一个替代方案吗?
最后我只是异步处理 audioQueue。但我认为 AVAudioUint
对于我的情况来说可能是更好的解决方案。
- (void)playBuffer:(AudioBuffer *)buffer format:(const AudioStreamBasicDescription *)format
{
oldAudioQueue = _audioQueue;
if (oldAudioQueue){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
AudioQueuePause(oldAudioQueue);
//AudioQueueStop(oldAudioQueue, YES);//this line consuming too much time, will barries thread
AudioQueueDispose(oldAudioQueue, YES);
oldAudioQueue = nil;
});
}
AudioQueueRef newAudioQueue;
AudioQueueBufferRef queueBuffer;
AudioQueueNewOutput(format, audioQueueOutputCallback, (__bridge void*)self,
nil, nil, 0, &newAudioQueue);
OSStatus status;
status = AudioQueueAllocateBuffer(newAudioQueue, buffer->mDataByteSize, &queueBuffer);
memcpy(queueBuffer->mAudioData, buffer->mData, buffer->mDataByteSize);
queueBuffer->mAudioDataByteSize=buffer->mDataByteSize;
status = AudioQueueEnqueueBuffer(newAudioQueue, queueBuffer, 0, NULL);
Float32 gain=1.0;
AudioQueueSetParameter(newAudioQueue, kAudioQueueParam_Volume, gain);
AudioQueueStart(newAudioQueue, nil);
AudioQueueFreeBuffer(newAudioQueue, queueBuffer);
_audioQueue = newAudioQueue;
}
需要:我有一个音频队列和两个 AudioQueueBuffer
。我怎么玩
NO.2 AudioQueueBuffer
立即在 NO.1 AudioQueueBuffer
比赛中。
我试过 AudioQueueStop
或 AudioQueueReset
。处理时间很长,NO.2AudioQueueBuffer
玩的太晚了。
-(void)playBuffer:(AudioBuffer *)buffer format:(const AudioStreamBasicDescription *)format
{
AudioQueueStop(_audioQueue, YES);//this line consuming too much time
AudioQueueDispose(_audioQueue, YES);
AudioQueueRef newAudioQueue;
AudioQueueBufferRef queueBuffer;
AudioQueueNewOutput(format, audioQueueOutputCallback, (__bridge void*)self,
nil, nil, 0, &newAudioQueue);
OSStatus status;
status = AudioQueueAllocateBuffer(newAudioQueue, buffer->mDataByteSize, &queueBuffer);
memcpy(queueBuffer->mAudioData, buffer->mData, buffer->mDataByteSize);
queueBuffer->mAudioDataByteSize=buffer->mDataByteSize;
status = AudioQueueEnqueueBuffer(newAudioQueue, queueBuffer, 0, NULL);
Float32 gain=1.0;
AudioQueueSetParameter(newAudioQueue, kAudioQueueParam_Volume, gain);
AudioQueueStart(newAudioQueue, nil);
AudioQueueFreeBuffer(newAudioQueue, queueBuffer);
_audioQueue = newAudioQueue;
}
所以我的问题 is:Is 可以 audio queue
立即播放下一个音频缓冲区吗?或者 audio queue
不匹配此任务我需要一个替代方案吗?
最后我只是异步处理 audioQueue。但我认为 AVAudioUint
对于我的情况来说可能是更好的解决方案。
- (void)playBuffer:(AudioBuffer *)buffer format:(const AudioStreamBasicDescription *)format
{
oldAudioQueue = _audioQueue;
if (oldAudioQueue){
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
AudioQueuePause(oldAudioQueue);
//AudioQueueStop(oldAudioQueue, YES);//this line consuming too much time, will barries thread
AudioQueueDispose(oldAudioQueue, YES);
oldAudioQueue = nil;
});
}
AudioQueueRef newAudioQueue;
AudioQueueBufferRef queueBuffer;
AudioQueueNewOutput(format, audioQueueOutputCallback, (__bridge void*)self,
nil, nil, 0, &newAudioQueue);
OSStatus status;
status = AudioQueueAllocateBuffer(newAudioQueue, buffer->mDataByteSize, &queueBuffer);
memcpy(queueBuffer->mAudioData, buffer->mData, buffer->mDataByteSize);
queueBuffer->mAudioDataByteSize=buffer->mDataByteSize;
status = AudioQueueEnqueueBuffer(newAudioQueue, queueBuffer, 0, NULL);
Float32 gain=1.0;
AudioQueueSetParameter(newAudioQueue, kAudioQueueParam_Volume, gain);
AudioQueueStart(newAudioQueue, nil);
AudioQueueFreeBuffer(newAudioQueue, queueBuffer);
_audioQueue = newAudioQueue;
}