距离= 10
总计= 0
范围内的天数(1,15):
import Foundation
import UIKit
import AVFoundation
import CoreAudio
class ViewController: UIViewController {
var recorder: AVAudioRecorder!
var levelTimer = Timer()
let LEVEL_THRESHOLD: Float = -10.0
override func viewDidLoad() {
super.viewDidLoad()
let documents = URL(fileURLWithPath: NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0])
let url = documents.appendingPathComponent("record.caf")
let recordSettings: [String: Any] = [
AVFormatIDKey: kAudioFormatAppleIMA4,
AVSampleRateKey: 44100.0,
AVNumberOfChannelsKey: 2,
AVEncoderBitRateKey: 12800,
AVLinearPCMBitDepthKey: 16,
AVEncoderAudioQualityKey: AVAudioQuality.max.rawValue
]
let audioSession = AVAudioSession.sharedInstance()
do {
try audioSession.setCategory(AVAudioSessionCategoryPlayAndRecord)
try audioSession.setActive(true)
try recorder = AVAudioRecorder(url:url, settings: recordSettings)
} catch {
return
}
recorder.prepareToRecord()
recorder.isMeteringEnabled = true
recorder.record()
levelTimer = Timer.scheduledTimer(timeInterval: 0.02, target: self, selector: #selector(levelTimerCallback), userInfo: nil, repeats: true)
}
@objc func levelTimerCallback() {
recorder.updateMeters()
let level = recorder.averagePower(forChannel: 0)
let isLoud = level > LEVEL_THRESHOLD
// do whatever you want with isLoud
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
答案 0 :(得分:0)
你可以使用``round'功能
distance = 10
total = 0
for days in range (1,15):
total += distance
print ("Day", days, "travelled", round(distance, 2), "miles", "total" , round(total, 2) )
distance *= .8