在切换多个视图控制器时,如何在 didMoveToView() 中调用一次歌曲?

How to call a song in didMoveToView(), once, when switching through several view controllers?

我的问题与这位朋友一年前提出的问题类似。不同的是我无法修复它。

How can I continuously play background music while switching to different views using swift?

一位用户这样回答了他的问题: “您可以从 AppDelegate 启动和停止音乐...最好的方法是创建一个 MusicPlayer Class,在 AppDelegate 中实例化它并调用其中的启动和停止方法...

你有足够的经验来写这样的东西吗?

要不我帮你?

也许我需要编辑应用委托?下面是我的 viewDidLoad 函数的样子。这是我调用音频的地方,但每次我加载视图控制器时,除了第一次加载它之外,它还会加载并播放歌曲。无论我在哪个视图中,如何播放将加载到视图控制器中并在应用程序 运行 时播放的歌曲?

    override func viewDidLoad() {
        super.viewDidLoad()

            let path = NSBundle.mainBundle().pathForResource("Theme", ofType: "wav")

            let soundURL = NSURL(fileURLWithPath: path!)

            do {
                try theme = AVAudioPlayer(contentsOfURL: soundURL)
                theme.prepareToPlay()
           } catch let err as NSError {
                print(err.debugDescription)
           }
// trying to make the music refrain from playing simultaneously
             if !theme.playing {
                theme.play()
           }

                theme.numberOfLoops = -1
        }

只需将所有音频播放器代码放入您的应用委托文件中的 didFinishLaunchingWithOptions 方法中。这只会在应用程序首次启动时播放音频文件一次,直到被告知不要播放为止。