音频队列不播放声音



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

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

请帮帮我。

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 = %dn",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];

如果您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
}

错误检查可能有助于发现问题所在。

您可能应该首先初始化共享音频会话,如下所述-https://stackoverflow.com/a/44805489/57449

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

相关内容

  • 没有找到相关文章

最新更新