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

1 回答 1

0

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

于 2010-12-17T10:45:55.417 回答