如何设置60秒的录音限制

时间:2015-11-18 09:26:32

标签: ios swift audio ios9 xcode7

您好我正在使用IQAudioRecorder进行录音。我想设置录制的修复时间,用户可以记录最多1分钟。那我怎么能这样做呢?

这是我的代码

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")
    }
}

3 个答案:

答案 0 :(得分:2)

使用NSTimer。录制开始时实例化NSTimer。 在60秒结束时,计时器将触发并调用一个停止录制的方法。

以下是示例代码:

    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.
}

答案 1 :(得分:0)

现在有一个允许的最大录制长度的属性。对于您的代码:

controller.maximumRecordDuration = 60

发布/解决方案的链接 - https://github.com/hackiftekhar/IQAudioRecorderController/issues/17#issuecomment-205175475

答案 2 :(得分:0)

我认为您忽略了代码的记录部分,该部分可能在IQAudioRecorderController中。

但是假设您创建了一个类似于以下内容的记录器:

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

您可以将最后一行更改为以下内容以记录一分钟:

audioRecorder.record(forDuration: 60.0)