簡體   English   中英

如何設置錄音的 60 秒限制

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

嗨,我正在使用 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")
    }
}

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

現在有最大允許記錄長度的屬性。 對於您的代碼:

控制器.maximumRecordDuration = 60

問題/解決方案鏈接——https: //github.com/hackiftekhar/IQAudioRecorderController/issues/17#issuecomment-205175475

我認為您遺漏了代碼的錄音部分,該部分可能位於 IQAudioRecorderController 中。

但假設您創建了一個類似於以下內容的記錄器:

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

您可以將最后一行更改為以下內容以記錄一分鍾:

audioRecorder.record(forDuration: 60.0)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM