【问题标题】:Problem using AVAudioRecorder使用 AVAudioRecorder 的问题
【发布时间】:2010-05-10 07:31:19
【问题描述】:

我在使用 AVAudioRecorder 时遇到了一个奇怪的问题。在我的应用程序中,我需要录制音频并播放它。我将我的播放器创建为:

if(recorder)
{
if(recorder.recording)
[recorder stop];
[recorder release];
recorder = nil;
}

NSString * filePath = [NSHomeDirectory() stringByAppendingPathComponent: [NSString stringWithFormat:@"Documents/%@.caf",songTitle]];
        NSDictionary *recordSettings = [[NSDictionary alloc] initWithObjectsAndKeys:
                                        [NSNumber numberWithFloat: 44100.0],AVSampleRateKey,
                                        [NSNumber numberWithInt: kAudioFormatAppleIMA4],AVFormatIDKey,
                                        [NSNumber numberWithInt: 1], AVNumberOfChannelsKey,
                                        [NSNumber numberWithInt: AVAudioQualityMax],AVEncoderAudioQualityKey,nil];
        recorder = [[AVAudioRecorder alloc] initWithURL: [NSURL fileURLWithPath:filePath] settings: recordSettings error: nil];
        recorder.delegate = self;
if ([recorder prepareToRecord] == YES){
                [recorder record];

每次按下录制按钮时,我都会释放和创建播放器。但问题是,AVAudiorecorder 在开始录制之前需要一些时间,所以如果我连续多次按下录制按钮,我的应用程序会冻结一段时间。 当耳机连接到设备时,相同的代码可以正常工作,没有任何问题......录制没有延迟,即使我多次按下录制按钮,应用程序也不会冻结。

如果有人指导我纠正此问题,将非常感激。 在这方面的任何帮助将不胜感激。

提前感谢。

【问题讨论】:

    标签: iphone avaudiorecorder


    【解决方案1】:

    找到解决方案。我刚刚添加了代码

    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;
            }
            err = nil;
            [audioSession setActive:YES error:&err];
            if(err){
                NSLog(@"audioSession: %@ %d %@", [err domain], [err code], [[err userInfo] description]);
                return;
            }
    

    它现在工作正常。但是我想知道这段代码有什么不同?以及为什么我的应用程序在连接耳机的情况下运行良好,但在没有耳机的情况下冻结了。

    【讨论】:

      【解决方案2】:

      是的,

      [audioSession setCategory :AVAudioSessionCategoryPlayAndRecord error:&err];
      

      是解决方案。它可以让您录制和播放音频。

      关于耳机报错,可能和属性有关

      [[AVAudioSession sharedInstance] inputIsAvailable]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-10-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-05-06
        相关资源
        最近更新 更多