录制和播放.wav文件IOS的问题

时间:2013-02-25 03:42:17

标签: ios ios5 ios6

我一直面临着录制音频并在IOS上播放它们的问题。

以下是我用于录制的内容:

    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    NSError *err = nil;
    [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord 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;
    }

    recordSettings = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                      [NSNumber numberWithFloat: 16000.0],AVSampleRateKey,
                      [NSNumber numberWithInt: kAudioFormatLinearPCM], AVFormatIDKey,// kAudioFormatLinearPCM
                      [NSNumber numberWithInt:8],AVLinearPCMBitDepthKey,
                      [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                      [NSNumber numberWithBool:NO],AVLinearPCMIsBigEndianKey,
                      [NSNumber numberWithBool:NO],AVLinearPCMIsFloatKey,
                      [NSNumber numberWithInt: AVAudioQualityLow],AVEncoderAudioQualityKey,
                      nil];

    recorderFilePath = [[NSString stringWithFormat:@"%@/%@", filePath, fileName] retain];
    NSLog(@"recorderFilePath: %@",recorderFilePath);
    NSURL *audioFileURL = [NSURL fileURLWithPath:recorderFilePath];

    recorder = [[ AVAudioRecorder alloc] initWithURL:audioFileURL settings:recordSettings error:&err];
    if(!recorder){
        NSLog(@"recorder: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
        UIAlertView *alert =
        [[UIAlertView alloc] initWithTitle: @"Warning"
                                   message: [err localizedDescription]
                                  delegate: nil
                         cancelButtonTitle:@"OK"
                         otherButtonTitles:nil];
        [alert show];
        [alert release];
        return;
    }

    //prepare to record
    [recorder setDelegate: self];
    [recorder prepareToRecord];
    recorder.meteringEnabled = YES;
    [recorder record];

对于我来说,使用上述设置的.wav文件中的录制工作正常。现在,问题在于回放。

以下是播放录制的.wav音频文件。

    NSURL * url = [NSURL fileURLWithPath:filePath];
    audioPlayer = [[[AVAudioPlayer alloc] initWithContentsOfURL:url
                                                  error:&err] autorelease];
    if (! audioPlayer) {
        NSLog(@"Sound named '%@' had error %@", name, [err localizedDescription]);
    } else {
        [audioPlayer prepareToPlay];  // Getting the return value NO here.
    }
   // audioPlayer.volume = 1; // Tried setting different values, but of no help.
   [audioPlayer play];  // Getting the return value NO here.

它不会抛出任何错误,但它什么都不做。没有声音播放。虽然可以使用播放器播放“.mp3”文件(来自资源的测试文件),但播放使用上述设置录制的文件是个问题。

我不确定我在这里缺少什么。请帮忙!感谢

2 个答案:

答案 0 :(得分:0)

AFAIK Apple的iOS不支持播放.wav格式,因为它主要是为windows media开发的格式。 WAV(Windows音频视频)是一种不压缩文件的媒体格式。即使iPad上的默认音乐播放器也不播放wav文件。

答案 1 :(得分:0)

问题解决了。

我在播放文件之前错过了停止录音机。

[recorder stop];

在播放前添加上述声明后修复了问题。