Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何设置iPhone AVAudioRecorder中的recordForDuration交叉后调用的函数?_Iphone_Avaudiorecorder - Fatal编程技术网

如何设置iPhone AVAudioRecorder中的recordForDuration交叉后调用的函数?

如何设置iPhone AVAudioRecorder中的recordForDuration交叉后调用的函数?,iphone,avaudiorecorder,Iphone,Avaudiorecorder,我正在使用AVAudioRecorder从麦克风录制一些声音。我正在使用[recorder recordForDuration:10]我想在10秒后上传声音文件。有没有办法具体说明这一点?任何帮助都将不胜感激 谢谢 - ahsan您需要设置AVAudioRecorder对象的委托 录制完成后,学员将收到消息audioRecorderDidFinishRecording:successfully: 请参阅AVAudioRecorder的文档: 和AVAudioRecorderDelegate:

我正在使用AVAudioRecorder从麦克风录制一些声音。我正在使用[recorder recordForDuration:10]我想在10秒后上传声音文件。有没有办法具体说明这一点?任何帮助都将不胜感激

谢谢

-
ahsan

您需要设置AVAudioRecorder对象的委托

录制完成后,学员将收到消息audioRecorderDidFinishRecording:successfully:

请参阅AVAudioRecorder的文档:

和AVAudioRecorderDelegate:


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


仅当您发送双精度、浮点或NSTimeInterval作为录制持续时间时,才可以使用代理录音机DiFinishRecording:successfully:

这些将触发代理

不会触发代理

从技术上讲,它们应该是NSTimeInterval类型,而不是float或double


来源:@Johnmph comment

记录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()

    }

}

这就是我所做的,添加了-(void)audiorecorderdifinishrecording:(AVAudioRecorder*)recorder成功:(BOOL)标志{NSLog(@“Done”);}。。但是,我没有得到任何输出:(…任何帮助?(我还添加了[self.recorder.delegate self];)以下任何一项都不起作用!!recorder.delegate=self;或[recorder setDelegate:self];委托函数只有在我显式使用[recorder-stop]…而不是在我使用[recorder-recordForDuration:10]时才起作用…有什么帮助吗?@Ahsan,recordForDuration需要NSTimeInterval(是一个双精度值)你传递了一个int值,也许这就是问题所在。如果这不起作用,你可以使用record代替recordForTime,并在创建一个nstimer后立即创建一个nstimer,它将在你选择的时间后调用stop方法。为什么要对我的答案进行向下投票,并将其与我的评论一起复制以生成一个新的答案?真正没有帮助的是复制/粘贴一个答案我不需要信用或来源,但我只是不喜欢你否决了我的答案。我的答案出了什么问题?录音结束后,你不需要通知一位代表吗?好吧,我可以说我的答案不完整,让你给出一个新的更完整的答案,但不要否决我的answer、 如果答案错误,请投反对票,如果答案不完整,请投反对票。我没有从你的战斗中学到任何东西,我测试了你所有的案例,没有一个案例触发了录音机。DifinishRecording:成功:
[recorder recordForDuration:10]; // int
    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()

    }

}