【问题标题】:How to resume a recording using AVAudioRecorder?如何使用 AVAudioRecorder 恢复录音?
【发布时间】:2011-07-02 11:48:58
【问题描述】:

我正在编写一个使用AVAudioRecorder 类的应用程序。除非有电话打进来,否则它工作得很好。我正在按照苹果使用 AVAudioRecorderDelegate 方法的指南处理这个

– (void) audioRecorderBeginInterruption:
– (void) audioRecorderEndInterruption:

在中断结束之前它工作得很好,我尝试通过再次调用 record 方法来“恢复”录制(根据文档)。但是,它不会恢复我的录制,而是会丢弃旧的并在其位置启动一个全新的。我还没有找到解决这个问题的方法,如果有人发现了这个问题,或者这是苹果AVAudioRecorder 的错误,请告诉我。我真的希望我不必使用AudioQueues 来写这篇文章。

谢谢

【问题讨论】:

    标签: iphone ios avaudiorecorder recording


    【解决方案1】:

    看起来它是苹果 API 的一个错误。很好玩……

    这是我们从支持票中收到的回复。

    “您描述的行为是一个错误,不幸的是,API 中没有任何内容可以更改以实际附加到原始录音。中断导致仅捕获中断后录制的音频。您可以尝试并在中断后停止录制,然后创建一个新文件,之后至少不会导致用户丢失任何信息,但结果将是两个单独的文件。

    请为此问题提交错误报告,因为外部开发人员提交的错误在 iOS 工程评估要解决的修复的关键功能时至关重要。它很容易重现,但如果你有一个可以包含的测试应用程序,请做,iOS 工程就像拥有直接显示错误的应用程序一样。 "

    【讨论】:

      【解决方案2】:

      我的解决方案是:

      1. 在临时文件上开始记录

      2. 注意 AVAudioSessionInterruptionNotificatio

      3. 在 AVAudioSessionInterruptionTypeBegan 上 - 停止录制。
      4. 在 AVAudioSessionInterruptionTypeEnded - 开始新录制。
      5. 当用户停止时 - 标记文件。

      Full Code

           [[NSNotificationCenter defaultCenter] addObserver:self
                                               selector:@selector(audioSessionInterruptionNotification:)
                                                   name:AVAudioSessionInterruptionNotification
                                                 object:audioSession];
      
      
      
          -(void)audioSessionInterruptionNotification:(NSNotification*)notification {
          NSString* seccReason = @"";
          //Check the type of notification, especially if you are sending multiple AVAudioSession events here
          NSLog(@"Interruption notification name %@", notification.name);
          NSError *err = noErr;
          if ([notification.name isEqualToString:AVAudioSessionInterruptionNotification]) {
          seccReason = @"Interruption notification received";
      
          //Check to see if it was a Begin interruption
          if ([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeBegan]]) {
              seccReason = @"Interruption began";
              NSLog(@"Interruption notification name %@ audio pause", notification.name);
      
              dispatch_time_t restartTime = dispatch_time(DISPATCH_TIME_NOW,
                                                          0.01 * NSEC_PER_SEC);
              dispatch_after(restartTime, dispatch_get_global_queue(0, 0), ^{
                  AVAudioRecorder *recorder = [[self recorderPool] objectForKey:lastRecID];
                  if (recorder) {
                      if(recorder.isRecording) {
                          [recorder stop];
                          NSLog(@"Interruption notification name Pauseing recording %@", lastRecID);
                      } else {
                          NSLog(@"Interruption notification name Already Paused %@", lastRecID);
                      }
                  }else {
                      NSLog(@"Interruption notification name recording %@ not found", lastRecID);
                  }
                    NSLog(@"Interruption notification Pauseing recording status %d",recorder.isRecording);
              });
      
          } else if([[notification.userInfo valueForKey:AVAudioSessionInterruptionTypeKey] isEqualToNumber:[NSNumber numberWithInt:AVAudioSessionInterruptionTypeEnded]]){
              seccReason = @"Interruption ended!";
               NSLog(@"Interruption notification name %@ audio resume", notification.name);
              //Start New Recording
              dispatch_time_t restartTime = dispatch_time(DISPATCH_TIME_NOW,
                                                          0.1 * NSEC_PER_SEC);
              dispatch_after(restartTime, dispatch_get_global_queue(0, 0), ^{
                  AVAudioRecorder *recorder = [[self recorderPool] objectForKey:lastRecID];
                  NSLog(@"Interruption notification Resumeing recording status %d",recorder.isRecording);
                  if (recorder) {
                      if(!recorder.isRecording) {
                          NSString *filePath = [[self orgFileNames] objectForKey:lastRecID];
                          NSArray * fileNames =[[self fileNames] objectForKey:lastRecID];
                          NSString *tmpFileName = [self gnrTempFileName:filePath AndNumber:fileNames.count];
                          [[[self fileNames] objectForKey:lastRecID] addObject:tmpFileName];
                          NSURL *url = [NSURL fileURLWithPath:tmpFileName];
                          NSError *error = nil;
                          recorder = [[AVAudioRecorder alloc] initWithURL:url settings:recordSetting error:&error];
                          if (![recorder record]) {
                              NSLog(@"Interruption notification Error Resumeing recording %@",tempRecorder);
                              return;
                          }
                          [[self recorderPool] setObject:recorder forKey:lastRecID];
                          NSLog(@"Interruption notification nameResumeing recording %@",lastRecID);
                      }else {
                           NSLog(@"Interruption notification Already Recording %d",recorder.isRecording);
                      }
                  }else {
                      NSLog(@"Interruption notification name recording %@ not found",lastRecID);
                  }
              });
            }
           } 
          }
      

      【讨论】:

        【解决方案3】:

        你会尝试使用这段代码

        -(IBAction)pauseandplay:(id)sender
        {
            BOOL status= [player isPlaying];
            if(status)
            {
            [pauseplay setImage:[UIImage imageNamed:@"play.png"]];
            [player pause];
            }
            else
            {
            [pauseplay setImage:[UIImage imageNamed:@"icon-pause.png"]];
            [player play];
            updateTimer = [NSTimer scheduledTimerWithTimeInterval:.01 target:self selector:@selector(updateCurrentTime) userInfo:player repeats:YES];
            }
        }
        

        【讨论】:

        • 不完全回答中断后恢复。
        猜你喜欢
        • 2012-07-24
        • 1970-01-01
        • 1970-01-01
        • 2010-11-03
        • 2011-08-04
        • 2019-06-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多