I have an app that needs to detect when the user is talking to the app. I don't want to have any third party APIs. I have used this turtorial, but I had to convert it to swift 2. This is the code that I am using:
import UIKit
import AVFoundation
import CoreAudio
class ViewController: UIViewController, AVAudioPlayerDelegate, AVAudioRecorderDelegate {
var audioRecorder: AVAudioRecorder?
var timer = NSTimer()
@IBAction func start(sender: AnyObject) {
if audioRecorder?.recording == false {
audioRecorder?.record()
timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(ViewController.checkForAudio), userInfo: nil, repeats: true)
}
}
@IBAction func stop(sender: AnyObject) {
if audioRecorder?.recording == true {
audioRecorder?.stop()
timer.invalidate()
}
}
func checkForAudio() {
audioRecorder?.updateMeters()
print("Average: \(audioRecorder?.averagePowerForChannel(0)) Peak: \(audioRecorder?.peakPowerForChannel(0))")
}
override func viewDidLoad() {
super.viewDidLoad()
let fileMgr = NSFileManager.defaultManager()
let dirPaths = fileMgr.URLsForDirectory(.DocumentationDirectory, inDomains: .UserDomainMask)
let soundFileURL = dirPaths[0].URLByAppendingPathComponent("sound.caf")
let recordSettings = [AVSampleRateKey: 44100.0, AVNumberOfChannelsKey: 2, AVEncoderBitRateKey: 12800, AVLinearPCMBitDepthKey: 16, AVEncoderAudioQualityKey: AVAudioQuality.Max.rawValue]
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
do {
try audioRecorder = AVAudioRecorder(URL: soundFileURL, settings: recordSettings as! [String : AnyObject])
audioRecorder?.prepareToRecord()
audioRecorder?.meteringEnabled = true
} catch let error as NSError {
print("audioSession error: \(error.localizedDescription)")
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
class func getDocumentsDirectory() -> String {
let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
let documentsDirectory = paths[0]
return documentsDirectory
}
}
Then in the log it just prints this and I am making loud sounds and taling but it still doesn't change.
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
Average: Optional(-120.0) Peak: Optional(-120.0)
I tried your code, your file URL is wrong in someway, I changed the file URL to a temporary path as:
let pathStr = NSTemporaryDirectory().stringByAppendingString("sound.caf")
let soundFileURL = NSURL(fileURLWithPath: pathStr)
and it works
Average: Optional(-27.2326279) Peak: Optional(-13.5865393)
Average: Optional(-33.1891823) Peak: Optional(-23.3389206)
Average: Optional(-49.0175018) Peak: Optional(-22.505867)
Average: Optional(-38.9896851) Peak: Optional(-18.3879299)
Average: Optional(-33.0929604) Peak: Optional(-20.2775478)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With