在多个场景中使用AVAudioPlayer Swift并能够调节音量 [英] Using AVAudioPlayer across multiple scenes Swift and be able to adjust volume
问题描述
您好,我想知道是否有一种方法可以在多个场景中使用AVAudioPlayer并更改音量.我目前将其放置在我的gameViewController中,后者是我所有应用程序场景的控制器:
Hi I was wondering if there was a way to use an AVAudioPlayer across multiple scenes and change the volume. I'm currently placing it in my gameViewController which is the controller for all of my application's scenes:
override func viewDidAppear(animated: Bool) {
let backgroundMusicURL = NSBundle.mainBundle().URLForResource("tempMusic.mp3", withExtension: nil)
backgroundMusicPlayer = AVAudioPlayer(contentsOfURL: backgroundMusicURL, error:nil)
backgroundMusicPlayer.numberOfLoops = -1
backgroundMusicPlayer.prepareToPlay();
backgroundMusicPlayer.play();
}
这可以按预期工作,但是我无法控制场景本身的音量.
This works as intended, but I am unable to control its volume in the scenes themselves.
那么,有没有一种方法可以播放背景音乐,使其在多个场景中流畅运行,并且在这些场景中仍可以调节音量?
So, is there a way to have background music playing so that it runs smoothly throughout multiple scenes and the volume is still adjustable from within these scenes?
推荐答案
好吧,我可以使用Matt提供的链接来使它正常工作(因此,该帖子的未来读者会对他的评论投赞成票).
Okay I got it to work using the link presented by Matt (so future viewers of this post up vote his comment).
if let vc = UIApplication.sharedApplication().keyWindow?.rootViewController as? GameViewController{
vc.setMusicVolume(Float(hexData.musicVolume)/100.0);
}
我现在从场景本身内部访问rootViewController.为了预防起见,我将其放在if语句中.
I now access the rootViewController from within the scenes themselves. I put it inside an if statement just as a precaution.
这篇关于在多个场景中使用AVAudioPlayer Swift并能够调节音量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!