Search code examples
iphonexcodeiphone-sdk-3.0avaudioplayeravaudiorecorder

audio files are not playing


I am creating an audio file and playing it. This is not working on the simulator and itouch. I dont know what I am missing in coding.

Creating the audio file:

[audioSession setCategory :AVAudioSessionCategoryRecord error:&err];
                if(err){
                  NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            return;
        }
        [audioSession setActive:YES error:&err];
        err = nil;
        if(err){
            NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
            return;
        }

        NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
        [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
        [recordSetting setValue:[NSNumber numberWithFloat:16000.0] forKey:AVSampleRateKey]; 
        [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

        ///****
        NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
        NSString* documentsDir = [paths objectAtIndex:0];
        recordedTmpFile = [NSURL fileURLWithPath:[documentsDir stringByAppendingPathComponent: [NSString stringWithFormat: @"%.0f.%@", [NSDate timeIntervalSinceReferenceDate] * 1000.0, @"caf"]]];
        ///****

        NSLog(@"Using File called: %@",recordedTmpFile);

        recorder = [[ AVAudioRecorder alloc] initWithURL:recordedTmpFile settings:recordSetting error:&error];

        AudioServicesCreateSystemSoundID((CFURLRef)recordedTmpFile, &_pewPewSound);
        [recorder setDelegate:self];
        [recorder prepareToRecord];
        [recorder record];

...and now playing the audio file:

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

    AVAudioPlayer * audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:recordedTmpFile error:&error];
    [audioPlayer setDelegate:self];
    [audioPlayer prepareToPlay];
    //[audioPlayer play];
    audioPlayer.numberOfLoops = 0;

    if (audioPlayer == nil)
        NSLog([error description]);             
    else 
        [audioPlayer play];
    AudioServicesPlaySystemSound(_pewPewSound);

Solution

  • there is usually a restriction on the number of seconds that can be played...it is actually on the size of the file! try playing a random "dong" or "bing" sound to verify your function is correct. Did you try that?