【问题标题】:how to set a function to be called after recordForDuration is crossed in iPhone AVAudioRecorder?如何设置在 iPhone AVAudioRecorder 中越过 recordForDuration 后调用的函数?
【发布时间】:2011-05-29 21:39:53
【问题描述】:

我正在使用 AVAudioRecorder 从麦克风录制一些声音。我正在使用 [recorder recordForDuration: 10] 我想在 10 秒结束时上传声音文件。有没有办法指定这个?任何帮助将不胜感激。

谢谢。

- 阿三

【问题讨论】:

    标签: iphone avaudiorecorder


    【解决方案1】:

    另一种方法是使用定时器功能! :)

    Xcode Objective-C | iOS: delay function / NSTimer help?

    【讨论】:

      【解决方案2】:

      你可以使用代理audioRecorderDidFinishRecording:successfully:只有当你发送一个double、float或NSTimeInterval作为录制的持续时间

      这些触发委托

      [recorder recordForDuration:10.0]; // double
      [recorder recordForDuration:10.0f]; // float
      

      不会触发委托

      [recorder recordForDuration:10]; // int
      

      从技术上讲,这些应该是 NSTimeInterval 类型,而不是浮点型或双精度型。

      来源:@Johnmph 评论

      【讨论】:

      • 为什么要否决我的答案并将其与我的评论一起复制以做出新的答案??
      • 真正无益的是,一年后复制/粘贴一个答案并否决这个答案。
      • 我不需要信用或来源,但我只是不喜欢你否决了我的答案。我的回答有什么问题?录制完成时不需要通知代表吗?可以说我的答案对于这种情况并不完整,并给出一个新的更完整的答案,但不能拒绝我的答案。如果答案不完整,则投反对票。
      • 我没有从你的战斗中学到任何东西,我测试了你所有的案例,没有一个会触发 audioRecorderDidFinishRecording:successfully:
      【解决方案3】:

      【讨论】:

      • 这是我所做的,添加了 - (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successful:(BOOL)flag {NSLog (@"Done");} .. 但是我没有得到任何输出:(.. 有什么帮助吗?(我还添加了 [self.recorder.delegate self];)
      • 以下均无效!! recorder.delegate=self;或 [记录器 setDelegate:self];
      • 只有当我明确使用 [recorder stop] 时,委托函数才起作用......如果我使用 [recorder recordForDuration: 10] 则不行......任何帮助?
      • @Ahsan,recordForDuration 需要一个 NSTimeInterval (它是一个双精度值)并且你传递一个 int 值,也许这就是问题所在。如果这也不起作用,您可以使用 record 代替 recordForTime 并在创建一个 nstimer 后立即在您选择的时间后调用 stop 方法
      【解决方案4】:

      用于录制 10 秒的 Swift 代码:

          var audioRecorder: AVAudioRecorder! 
          var meterTimer: NSTimer?
      
          func btnRecordAction(sender: UIButton) {
      
          let currentDateTime = NSDate()
          let formatter = NSDateFormatter()
          formatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
          let recordingName = formatter.stringFromDate(currentDateTime)+".m4a"
          let filePath = self.docPath!.stringByAppendingPathComponent(recordingName)
      
          var error: NSError?
          var session = AVAudioSession.sharedInstance()
          session.setCategory(AVAudioSessionCategoryPlayAndRecord, withOptions: .DuckOthers, error: &error)
      
          let recordSettings = [
              AVFormatIDKey: kAudioFormatMPEG4AAC,
              AVEncoderAudioQualityKey : AVAudioQuality.Max.rawValue,
              AVNumberOfChannelsKey: 1,
              AVSampleRateKey : 44100.0 
          ]
      
          let url = NSURL(fileURLWithPath: filePath)
          self.audioRecorder = AVAudioRecorder(URL: url, settings: recordSettings as! [NSObject : AnyObject] , error: &error)
          if let e = error {
              println("AVAudioRecorder error = \(e.localizedDescription)" )
          } else {
              self.audioRecorder.delegate = self
              self.audioRecorder.meteringEnabled = true
              self.audioRecorder.recordForDuration(10.0)
              self.audioRecorder.prepareToRecord()
              self.audioRecorder.record()
      
              if self.audioRecorder.recording == true {
                  self.meterTimer = NSTimer.scheduledTimerWithTimeInterval(0.1,
                      target:self,
                      selector:"updateAudioMeter:",
                      userInfo:nil,
                      repeats:true)
              }
          }
      }
      
      func audioRecorderDidFinishRecording(recorder: AVAudioRecorder!, successfully flag: Bool) {
              println("finished recording \(flag)")
      
      
              // ios8 and later
              var alert = UIAlertController(title: "Recorder",
                  message: "Finished Recording",
                  preferredStyle: .Alert)
              alert.addAction(UIAlertAction(title: "Keep", style: .Default, handler: {action in
                  println("keep was tapped")
              }))
              alert.addAction(UIAlertAction(title: "Delete", style: .Default, handler: {action in
                  self.audioRecorder?.deleteRecording()
              }))
              self.presentViewController(alert, animated:true, completion:nil)
      }
      
      func audioRecorderEncodeErrorDidOccur(recorder: AVAudioRecorder!,
          error: NSError!) {
              println("\(error.localizedDescription)")
      }
      
      func updateAudioMeter(timer:NSTimer) {
      
          if  self.audioRecorder.recording {
              let dFormat = "%02d"
              let min:Int = Int(self.audioRecorder.currentTime / 60)
              let sec:Int = Int(self.audioRecorder.currentTime % 60)
              let s = "Recording: \(String(format: dFormat, min)):\(String(format: dFormat, sec)) secs"
              self.lblRecorderTime!.text = s
              self.audioRecorder.updateMeters()
      
          }
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-05-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-07-11
        • 2010-11-03
        相关资源
        最近更新 更多