iosaudioaudioqueueservices

Can not get IOS InputAudioQueue to work (Very Simple Example)


I am doing some testing and learning on IOS audio queue service

I get a -50 osstatus when I try to create a new InputAudioQueue , what am I doing wrong?

I read apple AudioQueue service reference a couple of times , and read the function docs but I still don`t know whats the problem

Thanks

#import "AudioSenderVC.h"
#import <AudioToolbox/AudioToolbox.h>
#import <Foundation/Foundation.h>

@interface AudioSenderVC ()
@property (weak, nonatomic) IBOutlet UITextField *ipTextField;
@end

@implementation AudioSenderVC

static void MyAudioQueueInputCallback (
                                   void                                *inUserData,
                                   AudioQueueRef                       inAQ,
                                   AudioQueueBufferRef                 inBuffer,
                                   const AudioTimeStamp                *inStartTime,
                                   UInt32                              inNumberPacketDescriptions,
                                   const AudioStreamPacketDescription  *inPacketDescs
                                   )
{
NSLog(@"hey I just got audio data from the mic!");
}
void createAudioQueue(AudioStreamBasicDescription*  mDataFormat)
{
AudioQueueRef* inAQ = NULL;
OSStatus err=AudioQueueNewInput(mDataFormat, MyAudioQueueInputCallback ,NULL,CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, inAQ);
if(inAQ!=NULL)
    NSLog(@"YAY!, InputQueue was Created");
if(err)
    NSLog(@"Error:%i",(int)err);

 }
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
    // Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)setupAudioFormat:(AudioStreamBasicDescription*)format
{
format->mSampleRate = 8000.0;
format->mFormatID = kAudioFormatLinearPCM;
format->mFramesPerPacket = 1;
format->mChannelsPerFrame = 1;
format->mBytesPerFrame = 2;
format->mBytesPerPacket = 2;
format->mBitsPerChannel = 16;
format->mReserved = 0;
format->mFormatFlags = kLinearPCMFormatFlagIsBigEndian |
kLinearPCMFormatFlagIsSignedInteger |
kLinearPCMFormatFlagIsPacked;
}
- (IBAction)sendAudio:(id)sender {
AudioStreamBasicDescription*  mDataFormat=malloc(sizeof(AudioStreamBasicDescription));
[self setupAudioFormat:mDataFormat];
createAudioQueue(mDataFormat);
}

@end

Solution

  • You're passing NULL for the resulting queue, instead you should be passing the address of an AudioQueueRef, like this:

    AudioQueueRef inAQ;
    
    OSStatus err=AudioQueueNewInput(mDataFormat, MyAudioQueueInputCallback, NULL, CFRunLoopGetCurrent(), kCFRunLoopCommonModes, 0, &inAQ);