简体   繁体   English

如何设置录音的 60 秒限制

[英]How to set a 60 second limit to Audio recording

hi i am using IQAudioRecorder for audio recording.嗨,我正在使用 IQAudioRecorder 进行录音。 i want to set fix time for recording like user can record maximum upto 1 minut.我想设置录制的固定时间,就像用户最多可以录制 1 分钟一样。 so how can i do this?那我该怎么做呢?

Here is my code这是我的代码

func AudioFunction(){

    let controller = IQAudioRecorderController()

    controller.recordingTintColor = UIColor.whiteColor()
    controller.playingTintColor = UIColor.whiteColor()
    controller.delegate = self
    presentViewController(controller, animated: false, completion: nil)
}

func audioRecorderController(controller: IQAudioRecorderController!, didFinishWithAudioAtPath filePath: String!){
    let d = NSDate()
    print(d)
      let vData = NSData(contentsOfURL: NSURL(fileURLWithPath: filePath))
      dataStr = vData!.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)
      print(vData)
      print(filePath)

}
func audioRecorderControllerDidCancel(controller: IQAudioRecorderController!){

}


func playSound(soundName: String)
{

    let strpath = NSTemporaryDirectory() + "/" +  soundName
    NSLog("File: %@", strpath)
    let coinSound = NSURL(fileURLWithPath:strpath)

    do{
        audioPlayer = try AVAudioPlayer(contentsOfURL:coinSound)
        audioPlayer.prepareToPlay()
        audioPlayer.play()
    }catch {
        print("Error getting the audio file")
    }
}

Use an NSTimer.使用 NSTimer。 Instantiate an NSTimer when the recording starts.录制开始时实例化一个 NSTimer。 At the end of 60 seconds, the timer will fire and call a method, that stops recording.在 60 秒结束时,计时器将触发并调用停止记录的方法。

Here is the sample code :这是示例代码:

    func startRecording(){

    let theTimer = NSTimer.scheduledTimerWithTimeInterval(60, target: self, selector: Selector("stopRecording"), userInfo: nil, repeats: false)
    // Start recording here.

    theTimer.fire()
}

func stopRecording(){
    // Put the code to Stop recording here.
}

There is now a property for the maximum allowed recording length.现在有最大允许记录长度的属性。 For your code:对于您的代码:

controller.maximumRecordDuration = 60控制器.maximumRecordDuration = 60

Link to issue/resolution -- https://github.com/hackiftekhar/IQAudioRecorderController/issues/17#issuecomment-205175475问题/解决方案链接——https: //github.com/hackiftekhar/IQAudioRecorderController/issues/17#issuecomment-205175475

I think you left out the recording portion of your code, which may be in the IQAudioRecorderController.我认为您遗漏了代码的录音部分,该部分可能位于 IQAudioRecorderController 中。

But assuming that you have a recorder created similar to the below:但假设您创建了一个类似于以下内容的记录器:

audioRecorder = try AVAudioRecorder(url: audioFilename, settings: settings)
audioRecorder.delegate = self
audioRecorder.record()

You can change the last line to the following to record for one minute:您可以将最后一行更改为以下内容以记录一分钟:

audioRecorder.record(forDuration: 60.0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM