I am using the AVAudioRecorder to record some sound from the mic. I am using [recorder recordForDuration: 10] I want to upload the sound file when 10 second is over. Is there a way to specify this ? Any help will be appreciated.
Thanks.
- ahsan
I am using the AVAudioRecorder to record some sound from the mic. I am using [recorder recordForDuration: 10] I want to upload the sound file when 10 second is over. Is there a way to specify this ? Any help will be appreciated.
Thanks.
- ahsan
You can use the delegate audioRecorderDidFinishRecording:successfully:
only if you send a double, float or NSTimeInterval as the duration of the recording.
These will trigger the delegate
[recorder recordForDuration:10.0]; // double
[recorder recordForDuration:10.0f]; // float
This will not trigger the delegate
[recorder recordForDuration:10]; // int
Technically, these should be of the type NSTimeInterval and not float or double.
Source: @Johnmph comment
Another way will be to use a Timer function ! :)
You need to set the delegate of the AVAudioRecorder object.
The delegate will receive the message audioRecorderDidFinishRecording:successfully: when the recording is done.
See documentation of AVAudioRecorder :
and AVAudioRecorderDelegate :
Swift code for recording for 10 seconds:
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()
}
}