2010-12-17 251 views
0

我正在创建音频文件并播放它。这不适用于模拟器和itouch。我不知道我在编码中缺少什么。音频文件不播放

创建音频文件:

[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]; 

...现在播放音频文件:

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); 

回答

0

通常有上可以播放的秒数的限制...它实际上是文件的大小!尝试播放随机的“东”或“叮”声以验证您的功能是否正确。你尝试过吗?

+0

如果我将1个音频文件(.mp3)添加到资源路径,它会正常播放。我在创建音频文件时缺少某处 – xcodemaddy 2010-12-17 11:51:16