dimanche 12 avril 2015

SWIFT AVAudioPlayer multiple scenes in Game, stop not working


Vote count:

0




very similar to this question:


Using AVAudioPlayer across multiple scenes Swift and be able to adjust volume


I want to play BGmusic across multiple scenes, but i need it to STOP when i enter certain scenes.


So for example, all my menu scenes should play the music, but when i enter the GamePlayScene the current music should stop and new music should play.


I've implemented the fix in the above post, (changed it a bit for my needs) but it isn't working... what happens now is the BGmusic continues to play and then my game music plays also



if let vc = UIApplication.sharedApplication().keyWindow?.rootViewController as? GameViewController{
vc.menuMusicPlayer.stop()
println("stop the bg audio")
}


this println isn't even being executed - suggesting the if isn't true?


my GameViewController is parent to all my scenes...


not sure where I am going wrong here. Any advice?


GameViewController code - inside override func viewDidLoad{



var menuMusicPlayer:AVAudioPlayer = AVAudioPlayer()


var bgMusicURL:NSURL = NSBundle.mainBundle().URLForResource("BalloonPop99_menuSounds", withExtension: "m4a")!
menuMusicPlayer = AVAudioPlayer(contentsOfURL: bgMusicURL, error: nil)
menuMusicPlayer.volume = 0.25
menuMusicPlayer.numberOfLoops = -1
menuMusicPlayer.prepareToPlay()
if music_on == true && menuMusicPlayer.playing == false { menuMusicPlayer.play() }


and i also have a func to stop the music in GameViewController I've tried triggering the func or just telling menuMusicPlayer to stop() directly and neither are working.



func BGMusicStop() {
if menuMusicPlayer.playing == true {
menuMusicPlayer.stop()
println("bg music should stop")
}
}


asked 51 secs ago

Adam

11






SWIFT AVAudioPlayer multiple scenes in Game, stop not working

Aucun commentaire:

Enregistrer un commentaire