使用 iPodMusicPlayer 而不在后台控制 iPod

发布于 2024-10-30 17:43:52 字数 610 浏览 8 评论 0原文

我正在开发一个应用程序,其重点是在选定的播放列表中随机播放歌曲。当应用程序启动时,它会列出所有用户的播放列表,然后当选择一个播放列表时,它会将其加载到我的音乐播放器视图中并开始运行。我使用 MPMusicPlayerController iPodMusicPlayer 来处理此操作,因为我在 iOS 开发方面还很陌生,它可以处理后台和所有对我来说又好又容易的事情。我希望音乐在后台继续播放,并且我喜欢我在应用程序中所做的任何更改也会反映在 iPod 应用程序中。但是,我不希望用户能够更改 iPod 应用程序中的内容,然后返回到我的应用程序。由于我的应用程序主要是随机播放,因此用户可以轻松进入 iPod 并禁用随机播放,然后返回我的应用程序,体验就会被破坏。我也不希望他们从 iPod 中更改播放列表。

谁能帮我想个办法来实现这个目标?播放/暂停/跳过/上一首/音量在 iPod 内没有问题(我希望这是可能的),但我不希望更改歌曲队列成为可能。有没有一种方法可以在我的应用程序中添加一个处理程序,以捕获用户在我的应用程序处于后台时是否更改了某些内容(如果是这样,请停止音乐并将我的应用程序重置到初始屏幕)?我真的很感激任何建议。我希望在我冒险使用 AVPlayerQueue 或类似的东西之前这样的事情是可能的(顺便说一句,你能用 AVPlayerQueue 播放 DRM 文件吗?)。

I'm developing an app with a very specific focus on shuffling songs within a selected playlist. When the app launches, it lists all the user's playlists, and then when one is selected it loads it into my music player view and get's going. I'm using an MPMusicPlayerController iPodMusicPlayer to handle doing this because I'm pretty new at iOS development and it handles backgrounding and everything nice and easily for me. I want music to continue playing in the background and I like how whatever I change within my app is also reflected in the iPod app. However, I don't want the user to be able to change things in the iPod app and then return to mine. Since my app is all about shuffling, the user could easily go into iPod and disable shuffle, then come back to my app and the experience is ruined. I also don't want them to change the playlist from within iPod.

Can anyone help me think of a way to accomplish this? Play/pause/skip/prev/volume is no problem from within iPod (I want that to be possible), but I don't want changing the song queue to be possible. Is there a way I could add a handler in my app that catches if the user changed something while my app was in the background (and if so, stop music and reset my app to the initial screen)? I'd really appreciate any suggestions. I'm hoping something like this is possible before I venture into using AVPlayerQueue or the like (as a side note, can you play DRM'd files with AVPlayerQueue?).

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

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

发布评论

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

评论(1

忱杏 2024-11-06 17:43:52

我要做的是在后台设置一个布尔值:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:@"BACKGROUNDED"];

然后,在您应该实现的 MPMusicPlayerControllerNowPlayingItemDidChangeNotification 处理程序中,以便您拥有该控制权,执行以下操作:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs boolForKey:@"BACKGROUNDED"]) {
    [prefs setBool:NO forKey:@"BACKGROUNDED"];
    exit(0);
}

它可能太突然,具体取决于当它检查此代码时(我不知道它何时在您的项目中执行),但这应该可以实现您的目标。 exit(0) 是终止应用程序而不引发异常的方法。有帮助吗?

What I would do is set a bool when backgrounded:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setBool:YES forKey:@"BACKGROUNDED"];

Then, in your handler for the MPMusicPlayerControllerNowPlayingItemDidChangeNotification that you should be implementing so that you have that control, do the following:

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
if ([prefs boolForKey:@"BACKGROUNDED"]) {
    [prefs setBool:NO forKey:@"BACKGROUNDED"];
    exit(0);
}

It might be too abrupt, depending on when it checks this code (I don't know when it does in your project) but that should accomplish your goal. exit(0) is the way to terminate your app without throwing an exception. Is that helpful?

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