AVCaptureAudioDataOutput
SampleBufferDelegate,获取到了一连串的音频数据NSData,然后我想把这些NSData播放出来,要怎么转成音频流来播放呢,各位大虾给下思路。怎么实现才好呢?
openal播放音频流
不停的接受到buffer数据,然后不停的用openal去播放,为什么会声音有时候播放正常,有时候又是杂音呢
-(void)initOpenAL
{
mDevice=alcOpenDevice(NULL);
if(mDevice) {
mContext=alcCreateContext(mDevice,NULL);
alcMakeContextCurrent(mContext);
}
alGenSources(1,&outSourceID);
alSourcei(outSourceID,AL_LOOPING, AL_FALSE);
alSourcef(outSourceID,AL_SOURCE_TYPE, AL_STREAMING);
}
- (BOOL) updataQueueBuffer
{
ALintstateVaue;
intprocessed, queued;
alGetSourcei(outSourceID,AL_SOURCE_STATE, &stateVaue);
if(stateVaue == AL_STOPPED)
{
returnNO;
}
alGetSourcei(outSourceID,AL_BUFFERS_PROCESSED, &processed);
alGetSourcei(outSourceID,AL_BUFFERS_QUEUED, &queued);
NSLog(@"Processed= %d\n", processed);
NSLog(@"Queued= %d\n", queued);
while(processed--)
{
ALuintbuff;
alSourceUnqueueBuffers(outSourceID,1, &buff);
alDeleteBuffers(1,&buff);
}
returnYES;
}
- (void) openAudioFromQueue:(unsigned char*)datadataSize:(UInt32)dataSize index:(int)aIndex
{
ALenum error= AL_NO_ERROR;
if(data == NULL) {
return;
}
NSAutoreleasePool*pool = [[NSAutoreleasePool alloc] init];
NSCondition*ticketCondition= [[NSCondition alloc] init];
[ticketConditionlock];
[selfupdataQueueBuffer];
ALuintbufferID = 0;
alGenBuffers(1,&bufferID);
if((error= alGetError()) != AL_NO_ERROR) {
NSLog(@"erroralGenBuffers: %x\n", error);
}
else{
NSLog(@"sucalGenBuffers: %x\n", error);
NSLog(@"%s",data);
intsize = strlen( (const char *)data);
NSData* tmpData = [NSData dataWithBytes:data length:dataSize];
alBufferData(bufferID,AL_FORMAT_STEREO16, (const ALvoid*)[tmpData bytes],(ALsizei)[tmpData length], 44100);
if((error= alGetError()) != AL_NO_ERROR)
{
NSLog(@"errorsucalBufferData: %x\n", error);
}
else
{
NSLog(@"sucalBufferData:%x\n", error);
alSourceQueueBuffers(outSourceID,1, &bufferID);
if((error= alGetError()) != AL_NO_ERROR)
{
NSLog(@"erroralSourceQueueBuffers: %x\n", error);
}
else
{
NSLog(@"sucalSourceQueueBuffers: %x\n", error);
alSourcePlay(outSourceID);
if((error= alGetError()) != AL_NO_ERROR)
{
NSLog(@"erroralSourcePlay: %x\n", error);
alDeleteBuffers(1,&bufferID);
}
else
{
NSLog(@"sucalSourcePlay: %x\n", error);
}
}
}
}
[ticketConditionunlock];
[ticketConditionrelease];
ticketCondition= nil;
[poolrelease];
pool= nil;
}
我每次接受到buffer就调用openAudioFromQueue: ? alGenBuffers这个方法为什么在很多情况下都是返回error了,刚开始有时候返回a004,过一段时间就一直a003了,那位大大知道是什么原因吗?
是我申请的alGenBuffers次数太多,还是怎么了。
找到问题了:
ALintvalue;
alGetSourcei(outSourceID,AL_SOURCE_STATE,&value);
// NSLog(@"%x",value);
if(value != AL_PLAYING)
{
alSourcePlay(outSourceID);
}
调用play的时候没有判断是否在播放状态就去调用了,结果source又重新开始播放,一直卡那