使用AVAudioRecorder时出现问题 [英] Problem using AVAudioRecorder

查看:79
本文介绍了使用AVAudioRecorder时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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在开始录制之前需要花费一些时间,因此,如果我连续多次按下录制按钮,我的应用程序将冻结一段时间. 将耳机连接到设备时,相同的代码可以正常工作,没有任何问题……录音没有延迟,即使我多次按下录音按钮,应用也不会冻结.

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

先感谢

解决方案

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

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;
        }

,现在可以正常工作了.但是我想知道这段代码有什么区别?以及为什么我的应用程序在连接了耳机的情况下仍能正常工作,但没有耳机就冻结了吗?

I am facing a strange problem with AVAudioRecorder. In my application i need to record audio and play it. I am creating my player as :

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];

I am releasing and creating player every time i press record button. But the problem is that ,AVAudiorecorder is taking some time before starting to record , and so if i press record button multiple times continuously ,my application freezes for some time. The same code works fine without any problem when headphones are connected to device...there is no delay in recording, and the app doesn't freeze even if i press record button multiple times.

Will be greatful if anyone guides me in rectifying this issue. Any help in this regard will be highly appreciated.

Thanx in advance.

解决方案

Got the solution. I just added the code

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;
        }

and it is now working fine. However i want to know what difference does this piece of code made? And why it was like that my application was working fine with headphones connected but freezed without them.?

这篇关于使用AVAudioRecorder时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆