iOS:如何确保背景音乐一致播放和播放仅使用一个按钮即可停止所有 XIB?

发布于 2024-12-20 13:12:07 字数 500 浏览 3 评论 0原文

我有一款带有一个背景音乐按钮的 iPad 应用程序。背景音乐在主 XIB 和 XIB 中循环播放。第二届 XIB。基本上,我可以 on &成功关闭音乐。我现在的问题是,当我按下第二个 viewController 按钮时,我的背景音乐很混乱。我可以打开&关掉音乐但搞乱了之前主控制器的背景音乐。我该如何避免这种情况?有 Xcode4 的建议代码吗?

这是我在 mainController 中的示例代码。我将其复制到第二个视图控制器中,它会弄乱背景音乐。有什么好的建议代码吗?

 - (IBAction)toggleMusic {

        if ([self.player isPlaying] == YES) {
            [self.player stop]; 
        } else {
            [self.player play];
        }
        self.playBgMusic.enabled = YES;

    }

I have an iPad app with one background music button. The background music is looping all in the main XIB & the 2nd XIB. Basically, I can on & off the music successfully. My problem now is when i press the 2nd viewController button, my background music is mess up. I can on & off the music but mess up with the previous main controller's background music. How do I avoid that? Any suggestion code for Xcode4?

Here is my sample code in the mainController. I copy this in the 2nd viewController, it will mess up the background music. Any great suggestion code?

 - (IBAction)toggleMusic {

        if ([self.player isPlaying] == YES) {
            [self.player stop]; 
        } else {
            [self.player play];
        }
        self.playBgMusic.enabled = YES;

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

GRAY°灰色天空 2024-12-27 13:12:07

我不确定我是否理解正确,但是为什么不创建单例对象来管理声音呢?到时候你就有参考了。这似乎更合适,因为背景音乐更多地属于应用程序而不是单个视图(您在每个视图中使用它)。代码将如下所示:

 - (IBAction)toggleMusic {

    if ([[[BackgroundMusic sharedMusic] player] isPlaying] == YES) {
        [[[BackgroundMusic sharedMusic] player] stop]; 
    } else {
        [[[BackgroundMusic sharedMusic] player] play];
    }
    self.playBgMusic.enabled = YES;

}

I'm not sure if I understood you correctly, but why don't you create singleton object for managing sounds? You'll have the reference then. It seems more aprioprate as the background music belongs more to the application than to single view(you use it in each view). The code will look like this then:

 - (IBAction)toggleMusic {

    if ([[[BackgroundMusic sharedMusic] player] isPlaying] == YES) {
        [[[BackgroundMusic sharedMusic] player] stop]; 
    } else {
        [[[BackgroundMusic sharedMusic] player] play];
    }
    self.playBgMusic.enabled = YES;

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文