音频队列不播放声音

时间:2013-01-18 07:58:56

标签: core-audio audioqueue audioqueueservices

我正在制作使用音频队列的应用程序。 我通过阅读和学习书籍来制作这些代码。

我只想简单地播放音频文件。 它没有显示任何编译错误,但它没有发现。

请帮帮我。

AudioQueuePayer.h

@interface AudioQueuePlayer : NSObject{
    AudioQueueRef audioQueueObject;
    AudioFileID audioFileID;
    UInt32 numPacketsToRead;
    SInt64 startingPacketCount;
    BOOL donePlayingFile;
}

@property UInt32 numPacketsToRead;
@property AudioFileID audioFileID;
@property SInt64 startingPacketCount;
@property BOOL donePlayingFile;

-(void) prepareAudioQueue;
-(void)play;

AUdioQueuePlayer.m

-(void)prepareAudioQueue{
    //open Audio File
    NSString *path = [[NSBundle mainBundle] pathForResource: @"Arles_001_44"
                                                 ofType: @"wav"];
    NSURL *fileURL = [NSURL fileURLWithPath:path];

    AudioFileOpenURL((__bridge CFURLRef)fileURL,kAudioFileReadPermission,kAudioFileWAVEType,&audioFileID);

    //get audiostreamBasicDescription
    AudioStreamBasicDescription audioFormat;
    UInt32 size = sizeof(AudioStreamBasicDescription);
    AudioFileGetProperty(audioFileID,kAudioFilePropertyDataFormat,&size,&audioFormat);

    //making audioQueue Object
    AudioQueueNewOutput(&audioFormat,outputCallback,(__bridge void *)(self),NULL,NULL,0,&audioQueueObject);

    UInt32 maxPacketSize;
    UInt32 propertySize = sizeof(maxPacketSize);
    AudioFileGetProperty(audioFileID,kAudioFilePropertyPacketSizeUpperBound,
                     &propertySize,
                     &maxPacketSize);
    printf("maxPacketSize = %d\n",maxPacketSize);
    startingPacketCount = 0;
    AudioQueueBufferRef buffers[3];
    numPacketsToRead = 1024;
    UInt32 bufferByteSize = numPacketsToRead * maxPacketSize;
    NSLog(@"bufferByteSize = %d",bufferByteSize);
    int bufferIndex;
    for (bufferIndex = 0;bufferIndex < 3 ;bufferIndex++){

    //make buffers
        AudioQueueAllocateBuffer(audioQueueObject,bufferByteSize,&buffers[bufferIndex] );
        outputCallback((__bridge void *)(self),audioQueueObject,buffers[bufferIndex]);
        if (donePlayingFile)break;
    }

}

- (id)init{
    self = [super init];
    if (self != nil){
        [self prepareAudioQueue];
    }
    return self;
}

- (void)play{
    AudioQueueStart(audioQueueObject,NULL);
    NSLog(@"play Audio Queue");
}

void outputCallback(void *inUserData,
                AudioQueueRef inAQ,
                AudioQueueBufferRef inBuffer){
    //write herelater.
}

在其他班级

AudioQueuePlayer *AQP = [AudioQueuePlayer alloc];
[AQP play];

2 个答案:

答案 0 :(得分:0)

如果您init AudioQueuePlayer而非alloc,会发生什么?

AudioQueuePlayer *AQP [[AudioQueuePlayer alloc] init];

此外,大多数CoreAudio函数返回一个OSStatus错误代码,您可以检查,如:

OSStatus err;
err = AudioFileOpenURL((__bridge CFURLRef)fileURL,kAudioFileReadPermission,kAudioFileWAVEType,&audioFileID);
if (err != noErr) {
    //do something
}

错误检查可能有助于找出问题所在。

答案 1 :(得分:0)

可能是你应该首先使用init共享音频会话,如此处所述 - https://stackoverflow.com/a/44805489/57449

是的,为了从AudioQueue获取声音,您也应该初始化AudioSession。遇到类似的问题,在我的情况下,AudioSession只有在AVCaptureSession成功使用(内部初始化会话)后才会产生声音。