gpt4 book ai didi

ios - 检测与 swift 交谈

转载 作者:搜寻专家 更新时间:2023-10-31 22:45:54 26 4
gpt4 key购买 nike

我有一个应用需要检测用户何时与该应用通话。我不想有任何第三方 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)

最佳答案

我试过你的代码,你的文件 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)

关于ios - 检测与 swift 交谈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38191656/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com