在iPhone AVAudioRecorder中越过recordForDuration之后如何设置要调用的函数?

时间:2011-05-29 21:39:54

标签: iphone avaudiorecorder

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

感谢。

- 阿赫桑

4 个答案:

答案 0 :(得分:3)

另一种方法是使用Timer功能! :)

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

答案 1 :(得分:3)

只有当您发送double,float或NSTimeInterval作为录制的持续时间时,才可以使用委托audioRecorderDidFinishRecording:successfully:

这些触发代理

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

不会触发代理

[recorder recordForDuration:10]; // int

从技术上讲,这些应该是NSTimeInterval类型,而不是float或double。

来源:@Johnmph评论

答案 2 :(得分:1)

答案 3 :(得分:0)

用于录制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()

    }

}