0

I am making application which uses Audio Queue. I made these code by reading and studying from books.

I just want to play the audio file simply. it doesnt show any compile errors,but it doesn't soound.

please help me.

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.
}

in other class

AudioQueuePlayer *AQP = [AudioQueuePlayer alloc];
[AQP play];
ninesided
  • 23,085
  • 14
  • 83
  • 107
whitebear
  • 11,200
  • 24
  • 114
  • 237
  • Your `outputCallback` is not implemented, but I don't know if you've just omitted it from your question or if you've actually not written it yet. This has to be functional for sound to play. – iluvcapra Feb 28 '13 at 02:44

2 Answers2

0

what happens if you init your AudioQueuePlayer instead of just alloc ?

AudioQueuePlayer *AQP [[AudioQueuePlayer alloc] init];

Besides, most of CoreAudio functions return an OSStatus error code that you might check, like :

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

Errors checking might help in finding what goes wrong.

audionuma
  • 333
  • 1
  • 9
  • 15
  • HI audionuma,thanks. I have tried [[AudioQueuePlayer alloc] init]; But nothing changes. Then I use check code for every functions However there no errors appear...... It looks to work perfect but no sounds... OSStatus err; err = AudioFileOpenURL((__bridgeCFURLRef)fileURL,kAudioFileReadPermission,kAudioFileWAVEType,&audioFileID); if (err != noErr) { NSLog(@"error1"); } err = AudioFileGetProperty~~ err = AudioFileOpenURL((~~ err = AudioQueueNewOutput(~~ err = AudioFileGetProperty(~~ err = AudioQueueAllocateBuffer~~ err = AudioQueueStart~~ – whitebear Jan 21 '13 at 13:18
0

May be you should init shared audio session first, like described here - https://stackoverflow.com/a/44805489/57449

Yes, in order to get sound from AudioQueue you should initialize AudioSession too. Stucked with the similar problem, in my case AudioSession produced sound only after AVCaptureSession succesfull use (which initializes session internally).

IPv6
  • 405
  • 4
  • 17