Objective-C录制,暂停录制,播放暂停录制,继续录制

时间:2016-10-31 13:35:00

标签: ios objective-c xcode avaudioplayer avaudiorecorder

我正在尝试制作一个表现如下的录音机

  1. 录制语音
  2. 暂停声音
  3. 收听录音
  4. 继续录制
  5. 我试过这样做:

      

    初始化记录器

    NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
    NSString *dataPath = [cacheDir stringByAppendingPathComponent:@"/AudioFiles"];
    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
    {
        NSError *error=nil;
        [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
    }
    NSArray *pathComponents = [NSArray arrayWithObjects:
                               dataPath,
                               [NSString stringWithFormat:@"%@.m4a",self.FileName],
                               nil];
    NSURL *outputFileURL = [NSURL fileURLWithPathComponents:pathComponents];
    
    // Setup audio session
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    // Define the recorder setting
    NSMutableDictionary *recordSetting = [[NSMutableDictionary alloc] init];
    
    [recordSetting setValue:[NSNumber numberWithInt:kAudioFormatMPEG4AAC] forKey:AVFormatIDKey];
    [recordSetting setValue:[NSNumber numberWithFloat:22050.0] forKey:AVSampleRateKey];
    [recordSetting setValue:[NSNumber numberWithFloat:32000.0] forKey:AVEncoderBitRateKey];
    [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];
    [recordSetting setValue:AVAudioBitRateStrategy_Variable forKey:AVEncoderBitRateStrategyKey];
    // Initiate and prepare the recorder
    recorder = [[AVAudioRecorder alloc] initWithURL:outputFileURL settings:recordSetting error:NULL];
    recorder.delegate = self;
    recorder.meteringEnabled = YES;
    [recorder prepareToRecord];
    
      

    记录

    [recorder record];
    
      

    暂停记录

    [recorder pause];
    
      

    播放记录

    playingSession = [AVAudioSession sharedInstance];
    [playingSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [playingSession setActive: YES error:nil];
    NSError *error = nil;
    player = [[AVAudioPlayer alloc] initWithContentsOfURL:recorder.url error:&error];
    NSLog(@"Error initializing player: %@", error);
    [player setDelegate:self];
    [player play];
    

    所以我收到以下错误

      

    错误域= NSOSStatusErrorDomain代码= 1685348671"(null)"

    我知道如何才能使这项工作?

0 个答案:

没有答案