音频文件没有播放

时间:2010-12-17 10:37:56

标签: iphone xcode iphone-sdk-3.0 avaudioplayer avaudiorecorder

我正在创建一个音频文件并播放它。这不适用于模拟器和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);

1 个答案:

答案 0 :(得分:0)

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