Reputation: 12433
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];
Upvotes: 0
Views: 1032
Reputation: 435
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).
Upvotes: 0
Reputation: 343
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.
Upvotes: 0