我有一个应用程序需要检测用户何时与应用程序通信。我不想要任何第三方API。我使用了this turtorial,但我必须将它转换为swift 2.这是我正在使用的代码:
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
}
}
然后在日志中它只是打印出来,我正在发出响亮的声音和声音,但它仍然没有改变。
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)
答案 0 :(得分:2)
我尝试了你的代码,你的文件URL错了,我将文件URL更改为临时路径:
let pathStr = NSTemporaryDirectory().stringByAppendingString("sound.caf")
let soundFileURL = NSURL(fileURLWithPath: pathStr)
并且有效
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)