如何保存当前 MPMediaItem 的状态并在进入前台时响应更改的值
所以我正在尝试开发一款使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)