回到创作者viewController时背景音乐重叠

时间:2016-02-19 17:02:23

标签: ios swift uiviewcontroller avfoundation

我有一个由3个视图控制器组成的游戏:

  • 的viewController
  • settingViewController
  • GameViewController

我已经设置了背景音乐并在viewController类中播放了它

var backgroundMusic : AVAudioPlayer!
func setUpSounds(){
        //button sound
        if let buttonSoundPath = NSBundle.mainBundle().pathForResource("buttonClick", ofType: "mp3") {
            let buttonSoundURL = NSURL(fileURLWithPath: buttonSoundPath)
            do {
                try buttonSound = AVAudioPlayer(contentsOfURL: buttonSoundURL)
            }catch {
                print("could not setup button sound")
            }
            buttonSound.volume = 0.5
        }
        //background sound
        if let backgroundMusicPath = NSBundle.mainBundle().pathForResource("BackgroundMusic", ofType: "mp3") {
            let backgroundMusicURL = NSURL(fileURLWithPath: backgroundMusicPath)
            do {
                try backgroundMusic = AVAudioPlayer(contentsOfURL: backgroundMusicURL)
            }catch {
                print("could not setup background music")
            }
            backgroundMusic.volume = 0.2
            /*
            set any negative integer value to loop the sound
            indefinitely until you call the stop method
            */
            backgroundMusic.numberOfLoops = -1
        }
    }


override func viewDidLoad() {
        super.viewDidLoad()
        self.setUpSounds()
        self.playBackgroundSound()
        // Do any additional setup after loading the view, typically from a nib.
    }

当我移动到settingViewController然后再返回到viewController时,声音重放并重叠旧的播放音乐。

这个问题的解决方案是什么?

1 个答案:

答案 0 :(得分:0)

在致电AVAudioPlayer之前,您必须检查playBackgroundSound是否正在播放音乐。

您还可以将SoundManager作为单身人士,这样您就可以从应用程序的其他部分对其进行操作。

    class SoundManager{
        static var backgroundMusicSharedInstance = AVAudioPlayer?
    }

在视图中

    func setUpSounds(){
            //background sound

       if SoundManager.backgroundMusicSharedInstance == nil {
            if let backgroundMusicPath = NSBundle.mainBundle().pathForResource("BackgroundMusic", ofType: "mp3") {
                let backgroundMusicURL = NSURL(fileURLWithPath: backgroundMusicPath)
                do {
                    try SoundManager.backgroundMusicSharedInstance = AVAudioPlayer(contentsOfURL: backgroundMusicURL)
                }catch {
                    print("could not setup background music")
                }
                SoundManager.backgroundMusicSharedInstance!.volume = 0.2
                /*
                set any negative integer value to loop the sound
                indefinitely until you call the stop method
                */
                SoundManager.backgroundMusicSharedInstance!.numberOfLoops = -1
            }
        }
}


    override func viewDidLoad() {
            super.viewDidLoad()
            self.setUpSounds()
             if SoundManager.backgroundMusicSharedInstance!.playing == false{
                  self.playBackgroundSound()
              }    
    }