更改锁屏背景音频控制文本?

27

我有一个使用AVAudioSession流式传输背景音频的iOS应用程序。它正常工作,但我很好奇,是否有办法更改锁屏音频控件上的文本?现在它只显示我的应用程序名称,但我想将其更改为曲目名称。

此外,多任务栏在控件下面没有文本 - 是否有方法添加曲目名称,就像iPod应用程序一样?


有人知道如何使用Web音频来实现吗? - Justin Thomas
2个回答

33

iOS 5现在支持在锁屏和远程播放控制(双击Home按钮并向右滑动时出现的控制器)中设置曲目标题和专辑封面图像。请查看MPNowPlayingInfoCenter类。当然,为了最大限度的兼容性,您需要测试是否可用MPNowPlayingInfoCenter类,可以通过进行以下操作来实现:

if ([MPNowPlayingInfoCenter class])  {
   /* we're on iOS 5, so set up the now playing center */
   UIImage *albumArtImage = [UIImage imageNamed:@"HitchHikersGuide"];
   albumArt = [[MPMediaItemArtwork alloc] initWithImage:albumArtImage];

    NSDictionary *currentlyPlayingTrackInfo = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"Life, the Universe and Everything", [NSNumber numberWithInt:42], albumArt, nil] forKeys:[NSArray arrayWithObjects:MPMediaItemPropertyTitle, MPMediaItemPropertyPlaybackDuration, MPMediaItemPropertyArtwork, nil]];
    [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = currentlyPlayingTrackInfo;
}

8
您需要在此处使用classFromString,因为这将导致较低版本的操作系统崩溃。 - coneybeare
没有。在 iOS 4.x 中没有官方的设置方法。我甚至认为没有非官方的方法可以做到这一点。 - Donald Burr

4

这是用Swift的实现方式!(无需再检查是否支持iOS 5及以上版本)

let albumArt = MPMediaItemArtwork(image: UIImage(named:"HitchHikersGuide"))
let albumDict = [MPMediaItemPropertyTitle: "Life, the Universe and Everything", MPMediaItemPropertyPlaybackDuration: 42, MPMediaItemPropertyArtwork: albumArt]
MPNowPlayingInfoCenter.defaultCenter().nowPlayingInfo = albumDict

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接