在iOS设备的锁屏上添加自定义专辑封面

时间:2014-03-27 18:37:50

标签: ios iphone mpmediaitem

我正在构建一个流播shoutcast音频的iOS应用。

我想在设备锁屏上显示自定义图像(无论歌曲是什么,我希望显示相同的图像)。

下面的代码向我展示了锁屏上的当前标题,但没有像下面的图像那样显示专辑封面。

首先是可能的(我认为,Mixcloud实现了它)?如果是的话,这有什么问题?来源这里。

Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
    NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
    UIImage * albumImage = [UIImage imageNamed: @"icon_HD.png"];
    MPMediaItemArtwork * albumArt = [[MPMediaItemArtwork alloc] initWithImage:albumImage];
    [songInfo  setObject:titre.text          forKey:MPMediaItemPropertyTitle];
    [songInfo  setObject:artiste.text        forKey:MPMediaItemPropertyArtist];
    [songInfo  setObject:albumArt            forKey:MPMediaItemPropertyArtwork];
    [[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}

https://lh6.googleusercontent.com/-yV8VbLatlxg/UoD7Pq-Hv1I/AAAAAAAALVg/UEwKUp00k2U/s1600/iOS-7-lock-screen-music-controls.png

1 个答案:

答案 0 :(得分:6)

这是我在我的应用上使用的代码。适用于iOS7

UIImage *image = [UIImage imageNamed:@"myImage.png"];

MPMediaItemArtwork *albumArtwork = [[MPMediaItemArtwork alloc] initWithImage:image];

NSDictionary *info = @{ MPMediaItemPropertyTitle: @"Song Name",
                        MPMediaItemPropertyArtist: @"Artist Name",
                        MPMediaItemPropertyAlbumTitle: @"Album Name",
                        MPMediaItemPropertyPlaybackDuration: 1.0,
                        MPNowPlayingInfoPropertyElapsedPlaybackTime: 1.0,
                        MPMediaItemPropertyArtwork: albumArtwork };

[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = info;

让我知道是否适合你。