简体   繁体   中英

How to set a 60 second limit to Audio recording

hi i am using IQAudioRecorder for audio recording. i want to set fix time for recording like user can record maximum upto 1 minut. 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. Instantiate an NSTimer when the recording starts. At the end of 60 seconds, the timer will fire and call a method, that stops recording.

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

Link to issue/resolution -- 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.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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