如何保存当前 MPMediaItem 的状态并在进入前台时响应更改的值

发布于 2024-11-16 16:39:16 字数 1217 浏览 4 评论 0原文

所以我正在尝试开发一款使用 iPod 音乐库的音乐游戏。用户根据提示选择歌曲。由于我使用的是 [MPMusicPlayerController iPodMusicPlayer],因此用户可能在返回应用之前更改了 iPod 应用中的歌曲。如果是这种情况,我希望它调用 [musicPlayer stop]。不幸的是,我不知道如何保存当前播放的歌曲,并在应用程序从后台返回时将其与当前播放的歌曲进行检查。检查下面的代码。

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *persistantID = [musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyPersistentID];
    [prefs setValue:persistantID forKey:@"NOWPLAYING_ID"];

}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *persistantID = [prefs stringForKey:@"NOWPLAYING_ID"];
    if (persistantID == [musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyPersistentID]) {
    }
    else {
        [musicPlayer stop];
    }
    [prefs setValue:nil forKey:@"NOWPLAYING_ID"];

}

谁能帮我一下吗 非常感谢。

So I'm trying to develop a music game that uses the iPod music library. The user picks a song based on a prompt. Because I'm using [MPMusicPlayerController iPodMusicPlayer], it's possible the user changed the song in the iPod app before coming back to the app. If that's the case, I want it to call [musicPlayer stop]. Unfortunately, I can't figure out how to save the currently playing song and check it against the currently playing song when the app returns from background. Check the code below.

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *persistantID = [musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyPersistentID];
    [prefs setValue:persistantID forKey:@"NOWPLAYING_ID"];

}

And

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    MPMusicPlayerController *musicPlayer = [MPMusicPlayerController iPodMusicPlayer];
    NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
    NSString *persistantID = [prefs stringForKey:@"NOWPLAYING_ID"];
    if (persistantID == [musicPlayer.nowPlayingItem valueForProperty:MPMediaItemPropertyPersistentID]) {
    }
    else {
        [musicPlayer stop];
    }
    [prefs setValue:nil forKey:@"NOWPLAYING_ID"];

}

Can anyone give me a hand? Thanks so much.

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

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

发布评论

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

评论(1

╭⌒浅淡时光〆 2024-11-23 16:39:16
- (void)applicationWillResignActive:(UIApplication *)application    {
    self.mediaItemSavedWhenAppSuspended = [musicPlayer nowPlayingItem];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    MPMediaItem *nowPlayingItem = [musicPlayer nowPlayingItem];
    NSNumber *playingItem = [nowPlayingItem valueForProperty:MPMediaItemPropertyPersistentID];
    NSNumber *previousItem = [self.mediaItemSavedWhenAppSuspended valueForProperty:MPMediaItemPropertyPersistentID];

    if( [playingItem compare:previousItem] == NSOrderedSame )   {   //  same track still playing
   }
- (void)applicationWillResignActive:(UIApplication *)application    {
    self.mediaItemSavedWhenAppSuspended = [musicPlayer nowPlayingItem];
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    MPMediaItem *nowPlayingItem = [musicPlayer nowPlayingItem];
    NSNumber *playingItem = [nowPlayingItem valueForProperty:MPMediaItemPropertyPersistentID];
    NSNumber *previousItem = [self.mediaItemSavedWhenAppSuspended valueForProperty:MPMediaItemPropertyPersistentID];

    if( [playingItem compare:previousItem] == NSOrderedSame )   {   //  same track still playing
   }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文