【问题标题】:AVAudioRecorder & debugger stops with no signalAVAudioRecorder 和调试器在没有信号的情况下停止
【发布时间】:2011-10-31 21:40:42
【问题描述】:

我需要用 iPhone 麦克风录制音频流。 我正在使用这个东西的 AVAudioRecorder。

我在 init 中初始化 AVAR:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [[paths objectAtIndex:0] retain];
NSError *error;
BOOL isDir = NO;
if (! [[NSFileManager defaultManager] fileExistsAtPath:cachePath isDirectory:&isDir] && isDir == NO) {
    [[NSFileManager defaultManager] createDirectoryAtPath:cachePath withIntermediateDirectories:NO attributes:nil error:&error];
}

pathNewFile=  [cachePath stringByAppendingPathComponent:@"123.caf"];

NSLog(@"%@",pathNewFile);

NSDictionary *settings=[[NSDictionary alloc]initWithObjectsAndKeys:
                               [NSNumber numberWithFloat:44100.0f],AVSampleRateKey,
                                    [NSNumber numberWithInt:kAudioFormatAppleLossless],AVFormatIDKey,
                                    [NSNumber numberWithInt:1],AVNumberOfChannelsKey,
                                    [NSNumber numberWithInt:AVAudioQualityMax],AVEncoderAudioQualityKey, nil];
recorder=[[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:pathNewFile] settings:settings error:nil];
[recorder prepareToRecord];

当用户按下rec-button时我开始录制:

-(void)recButton:(id)sender{
    NSLog(@"start rec");
        [rootNotesButton setTitle:@"Stop" forState:UIControlStateNormal];
            [rootNotesButton removeTarget:self action:@selector(recButton:) forControlEvents:UIControlEventTouchUpInside];
            [rootNotesButton addTarget:self action:@selector(stopButton:) forControlEvents:UIControlEventTouchUpInside];
            [recorder record];
}

-(void)stopButton:(id)sender{
            NSLog(@"stop rec");
            [rootNotesButton setTitle:@"Play" forState:UIControlStateNormal];
            [rootNotesButton removeTarget:self action:@selector(stopButton:) forControlEvents:UIControlEventTouchUpInside];
            [rootNotesButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
            [recorder stop];
}

-(void)playButton:(id)sender{
        NSLog(@"play rec");
            [rootNotesButton setTitle:@"Stop" forState:UIControlStateNormal];
            [rootNotesButton removeTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
            [rootNotesButton addTarget:self action:@selector(stopPButton:) forControlEvents:UIControlEventTouchUpInside];

            player=[[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL URLWithString:pathNewFile] error:nil];
            [player play];
}

-(void)stopPButton:(id)sender{
        NSLog(@"Stop play");
            [rootNotesButton setTitle:@"Play" forState:UIControlStateNormal];
            [rootNotesButton removeTarget:self action:@selector(stopPButton:) forControlEvents:UIControlEventTouchUpInside];
            [rootNotesButton addTarget:self action:@selector(playButton:) forControlEvents:UIControlEventTouchUpInside];
            [player stop];
}

但是当我尝试在模拟器中运行我的项目时:我的调试器在字符串上停止: 录音机=[[AVAudioRecorder alloc] initWithURL:[NSURL URLWithString:pathNewFile] 设置:设置错误:无];没有任何信号或错误。

【问题讨论】:

    标签: objective-c ios ios-simulator avaudiorecorder


    【解决方案1】:

    问题似乎在于在模拟器中写信给cachePath。我试过了,它奏效了:

    #if TARGET_IPHONE_SIMULATOR
        NSString *cachePath = @"/tmp";
    #else
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *cachePath = [[paths objectAtIndex:0] retain];
    #endif
    

    你有几个内存泄漏:

    1. 您保留cachePath,但不释放它。实际上,一开始就没有理由保留它。

    2. 您正在使用+alloc/-initplayButton 中创建player,这意味着您拥有它但您没有发布它(至少不在发布的代码中)。如果您将其定义为 property,则需要使用 self.player = … 才能正确保留和释放它。

    3. 您正在使用+alloc/-init 创建settings,但没有释放它。没有理由这样做。你可以像这样创建它:

    NSDictionary *settings=[NSDictionary dictionaryWithObjectsAndKeys: [NSNumber numberWithFloat:44100.0f],AVSampleRateKey, [NSNumber numberWithInt:kAudioFormatAppleLossless],AVFormatIDKey, [NSNumber numberWithInt:1],AVNumberOfChannelsKey, [NSNumber numberWithInt:AVAudioQualityMax],AVEncoderAudioQualityKey, nil];

    您需要保留pathNewFile,因为stringByAppendingPathComponent 返回一个自动释放的字符串,并且您正在类的其他方法中使用它。

    【讨论】:

    • 谢谢。有用。对我知道那个。它只是我的程序的测试变体。我是从录音的测试方法写的。最后,我修复了所有错误和错误。感谢您的建议。
    猜你喜欢
    • 2017-02-19
    • 1970-01-01
    • 2013-12-06
    • 2017-11-16
    • 2020-09-04
    • 1970-01-01
    • 2015-09-26
    • 1970-01-01
    相关资源
    最近更新 更多