gpt4 book ai didi

ios - 找出 Music.app 中正在播放的歌曲

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:22:11 25 4
gpt4 key购买 nike

在 iOS 上,我的应用程序是否有办法找出音乐应用程序中当前正在播放的歌曲?例如,如果他们在使用我的应用程序时在后台播放歌曲,我可以获取有关该歌曲的信息吗?如果可以的话,有没有办法让我的应用程序在新歌开始播放时收到某种通知?谢谢!

最佳答案

有可能获得这样的一些信息:(还有许多其他 MPMediaItemProperties)至于问题中的第二个问题,我认为当您的应用程序处于后台状态时这是不可能的。

编辑:也许您可以在需要时每 xx 秒在后台调用下面的这段代码,并比较这些值以查看音乐是否确实改变了您自己。请注意,虽然您的应用程序在后台运行的时间有限,但在这段时间过去后,您将不会获得更新的值。

#import <MediaPlayer/MediaPlayer.h>

@property (nonatomic, strong) MPMediaItem *lastItem;

- (void)viewDidLoad
{
[super viewDidLoad];

NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:20.0 target:self selector:@selector(checkIfSongChanged) userInfo:nil repeats:YES];
[timer fire];
}

- (void)checkIfSongChanged
{
if ([[MPMusicPlayerController iPodMusicPlayer] playbackState] == MPMusicPlaybackStatePlaying)
{
MPMediaItem *nowPlayingMediaItem =
[[MPMusicPlayerController iPodMusicPlayer] nowPlayingItem];

if (self.lastItem && nowPlayingMediaItem != self.lastItem)
{
NSLog(@"New media item is: %@",nowPlayingMediaItem);
}

self.lastItem = nowPlayingMediaItem;

NSLog(@"Media item is: %@,",nowPlayingMediaItem);
}
}

AppDelegate.m:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
UIBackgroundTaskIdentifier __block task = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^
{
[[UIApplication sharedApplication] endBackgroundTask:task];
task = UIBackgroundTaskInvalid;
}];
}

关于ios - 找出 Music.app 中正在播放的歌曲,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24749339/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com