iOS:在后台更新媒体信息

2024-01-11

我目前在我的应用程序中使用 MPNowPlayingInfoCenter 来显示正在播放哪首歌曲,但我希望将 HTTP Live Streaming 合并到我的应用程序中,该应用程序将在后台出现任意数量的不同曲目。

有没有办法在应用程序处于后台时设置 nowPlayingInfo(在一段时间后调用函数?),即使应用程序从技术上讲实际上并不在后台运行?

或者有没有一种方法可以使用我的服务器上的简单调用来设置正在播放的信息,该调用将返回正确的信息 - 使用返回字符串或图像的 API 调用?

我知道这是可能的,因为 Songza 已经做到了,但也许他们已经获得了使用 Apple 某些私有方法的许可(如果这甚至是你可以做的事情的话)。


如果您正在使用AVPlayer类,并且您的应用程序的主要目的是播放音乐,那么您将能够在后台运行它,从而更新nowPlayingInfo当轨道改变时。

举个简单的例子:

- (void)viewDidLoad {
    [super viewDidLoad]

    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];
    [[AVAudioSession sharedInstance] setActive: YES error: nil];

    if ([[UIApplication sharedApplication] respondsToSelector:@selector(beginReceivingRemoteControlEvents)]){
        [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
        [self becomeFirstResponder];
        //These two steps are important if you want the user to be able to change tracks with remote controls (you'll have to handle the remote control events yourself).
    }
    self.yourPlayer = [[AVPlayer alloc] init];
}

注销您的远程控制事件dealloc method:

[[UIApplication sharedApplication] endReceivingRemoteControlEvents]

Change 所需的背景模式在你的信息表 to 应用程序播放音频

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

iOS:在后台更新媒体信息 的相关文章

随机推荐