0

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

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

请帮我。

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;

音频队列播放器.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];
4

2 回答 2

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
}

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

于 2013-01-20T08:40:46.017 回答
0

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

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

于 2017-09-06T11:54:18.727 回答