@IBAction func handleVolumeChange(_ sender: UISlider) {
player.volume = sender.value
}
let audioFiles: [String: String] = [
"": "",
"": "",
"": "",
"":"",
]
var players : [AVAudioPlayer] = []
var volume = 0.0
// init () {
// for (name, ext) in audioFiles {
// let a1 = setupAudioPlayerWithFile(file: name as NSString, type:ext as NSString)
// players.append(a1!)
// }
// }
//
// required init?(coder aDecoder: NSCoder) {
// fatalError("init(coder:) has not been implemented")
// }
func setVolume(v:Double) {
volume = v
for audioPlayer in players {
audioPlayer.volume = Float(volume)
}
}
func playSound(i:Int)
{
let audioPlayer = players[i]
audioPlayer.play()
}
func setupAudioPlayerWithFile(file:NSString, type:NSString) -> AVAudioPlayer? {
let path = Bundle.main.path(forResource: file as String, ofType: type as String)
let url = NSURL.fileURL(withPath: path!)
var audioPlayer:AVAudioPlayer?
do {
try audioPlayer = AVAudioPlayer(contentsOf: url)
audioPlayer!.prepareToPlay()
} catch {
print("Player not available")
}
return audioPlayer
}
outputVolume
всегда равно 0.5
, поэтому не забудьте это сделать:do {
try AVAudioSession.sharedInstance().setActive(true, options: [])
} catch {
print(error)
}
Timer.scheduledTimer(withTimeInterval: 0.2, repeats: true) {(timer) in
print(AVAudioSession.sharedInstance().outputVolume)
}