【问题标题】:Issues with recording and playing .wav files IOS录制和播放 .wav 文件 IOS 的问题
【发布时间】:2013-02-25 03:42:16
【问题描述】:

我在录制音频并在 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”文件(来自资源的测试文件),但播放使用上述设置录制的文件是个问题。

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

【问题讨论】:

    标签: ios ios5 ios6


    【解决方案1】:

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

    【讨论】:

    • 我认为,问题在于录音。我尝试了一个示例“.wav”文件,它很好。您认为需要更改设置才能很好地录制声音吗?
    • 这种情况下播放用AVAudioRecorder录制的文件只是这里的问题。
    【解决方案2】:

    问题解决了。

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

    [recorder stop];
    

    在播放前添加上述语句后修复了问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多