我确定我问的是一个简单的问题,但我刚刚开始编码...所以请怜悯我吧!我在Xcode 7 Swift 2.音乐播放器是AVAudioPlayer类型。如果我启动应用程序并移动音量滑块(键入UISlider),然后按下播放按钮它会崩溃应用程序,而在我按下播放后它完美地工作,即使音乐暂停了。我知道原因是因为volumeSlider func中的“musicPlayer.playing”。我尝试了很多If语句(例如“如果musicPaused等等。)但我无法让它工作。感谢您的帮助。这是音量滑块功能:
@IBAction func volumeSliderChanged(sender: AnyObject) {
musicPlayer.playing
musicPlayer.volume = volumeSlider.value
}
这是Play功能:
@IBAction func playPause(sender: AnyObject) {
var items = toolbar.items!
if musicPaused == false {
playPauseBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Pause, target: self, action: "playPause:")
items[0] = playPauseBtn
toolbar.setItems(items, animated: true)
musicPlayer.play()
musicPaused = true
}
else{
var items = toolbar.items!
playPauseBtn = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.Play, target: self, action: "playPause:")
items[0] = playPauseBtn
toolbar.setItems(items, animated: true)
musicPlayer.pause()
musicPaused = false
}
}
@IBAction func playPauseButton(sender: UIButton) {
let playBtn = sender as UIButton
if toggleState == 1 {
if isPlaying {
musicPlayer.play()
}else{
playMusic()
}
toggleState = 2
playBtn.setImage(UIImage(named:"100_pause.png"),forState:UIControlState.Normal)
} else {
musicPlayer.pause()
toggleState = 1
playBtn.setImage(UIImage(named:"100_play-arrow.png"),forState:UIControlState.Normal)
}
}
这是我的musicPlayer声明:
var musicPlayer: AVAudioPlayer = AVAudioPlayer()
答案 0 :(得分:0)
如果您将代码包含在' musicPlayer'被宣布。 崩溃后错误日志说了什么?
我建议使用MPVolumeView类来控制音频。它应该控制我认为你正在寻找的设备音量。我已将其他人的帖子附加到类似问题上。 资料来源:How to change ios volume in swift with help UISlider