gpt4 book ai didi

ios - 我如何对 Musik 的开关和按钮进行编程?

转载 作者:行者123 更新时间:2023-11-28 11:20:15 25 4
gpt4 key购买 nike

我想开发一个小应用程序。但是从几天开始,我就尝试制作一个 Musik off Button。也许你可以帮助我。我是这样开始的。

func MusikAN (sender: UIButton!) {
MusikEinUndAusSchalten.setBackgroundImage(MusikEin, forState: .Normal)
MusikEinUndAusSchalten.addTarget(self, action: "MusikAus:", forControlEvents: .TouchUpInside)
}
func MusikAus (sender: UIButton!){
MusikEinUndAusSchalten.setBackgroundImage(MusikAus, forState: .Normal)
MusikEinUndAusSchalten.addTarget(self, action: "MusikAN:", forControlEvents: .TouchUpInside)
audioPlayer.stop()
}

我用这个功能制作了 Musik。

func Musik (sender: UIButton) {

var alertsound = NSURL(fileURLWithPath: NSBundle.mainBundle().pathForResource("Blub1", ofType: "mp3")!)

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)
AVAudioSession.sharedInstance().setActive(true, error: nil)

var error: NSError?
audioPlayer = AVAudioPlayer(contentsOfURL: alertsound, error: &error)
audioPlayer.prepareToPlay()
audioPlayer.play()

}

但它不起作用。声音传来。当您按下“MusikAus”按钮时,您必须听不到任何音乐,而当您按下“MusikAn”按钮时,您必须再次听到音乐。

最佳答案

每次按下按钮时都调用 addTarget 是个坏主意(是 ADD,不是 SET)。就像第二次点击一样,您将同时调用这两种方法,甚至可能多次。因此,要么在添加新对象之前调用 removeTarget,要么更好地设置并使用按钮的选定状态在两种方法之间切换。另外,MusikAN 方法甚至没有调用音频播放器上的东西,所以停止后它应该如何继续播放?

关于ios - 我如何对 Musik 的开关和按钮进行编程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28859553/

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