IOS锁屏状态播放音乐时显示专辑信息和图片

先搁两张效果图:

 

 

 

 

 

 

 

 

 

 

 

 

其实核心的东西很简单就是设置[MPNowPlayingInfoCenter defaultCenter]的nowPlayingInfo。这是一个字典。把标题,作者及专辑图片信息一设置,自动就出来了。

if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
NSMutableDictionary * dict = [[NSMutableDictionary alloc] init];
[dict setObject:title forKey:MPMediaItemPropertyAlbumTitle];
[dict setObject:artist forKey:MPMediaItemPropertyArtist];

MPMediaItemArtwork * mArt = [[MPMediaItemArtwork alloc] initWithImage:img];
[dict setObject:mArt forKey:MPMediaItemPropertyArtwork];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = nil;
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
[dict release];
}

上面的if (NSClassFromString(@”MPNowPlayingInfoCenter”))避免了版本兼容问题,这个API貌似只出现在5里面。当然了还要检查你的AppDelegate是不是继承于UIResponder,有的是自动生成,有的人习惯手写,如果是继承于NSObject请改为 AppDelegate : UIResponder。
知道了上面的信息,就简单了,下一步就是怎么获取title,artist和img.就是一面一段代码了。

NSBundle* bundle = [NSBundle mainBundle];
NSString* path = [bundle bundlePath];
NSURL * fileURL=[NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/akon、be - you - with.mp3", path]];
AudioFileTypeID fileTypeHint = kAudioFileMP3Type;
NSString *fileExtension = [[fileURL path] pathExtension];
if ([fileExtension isEqual:@"mp3"]||[fileExtension isEqual:@"m4a"])
{
AudioFileID fileID = nil;
OSStatus err = noErr;

err = AudioFileOpenURL( (CFURLRef) fileURL, kAudioFileReadPermission, 0, &fileID );
if( err != noErr ) {
NSLog( @"AudioFileOpenURL failed" );
}
UInt32 id3DataSize = 0;
err = AudioFileGetPropertyInfo( fileID, kAudioFilePropertyID3Tag, &id3DataSize, NULL );

if( err != noErr ) {
NSLog( @"AudioFileGetPropertyInfo failed for ID3 tag" );
}
NSDictionary *piDict = nil;
UInt32 piDataSize = sizeof( piDict );
err = AudioFileGetProperty( fileID, kAudioFilePropertyInfoDictionary, &piDataSize, &piDict );
if( err != noErr ) {
[piDict release];
NSLog( @"AudioFileGetProperty failed for property info dictionary" );
}
CFDataRef AlbumPic= nil;
UInt32 picDataSize = sizeof(picDataSize);
err =AudioFileGetProperty( fileID, kAudioFilePropertyAlbumArtwork, &picDataSize, &AlbumPic);
if( err != noErr ) {
NSLog( @"Get picture failed" );
}
NSData* imagedata= (NSData*)AlbumPic;
UIImage* image=[[UIImage alloc]initWithData:imagedata];
imageView.image=image;
NSString * Album = [(NSDictionary*)piDict objectForKey:
[NSString stringWithUTF8String: kAFInfoDictionary_Album]];
NSString * Artist = [(NSDictionary*)piDict objectForKey:
[NSString stringWithUTF8String: kAFInfoDictionary_Artist]];
NSString * Title = [(NSDictionary*)piDict objectForKey:
[NSString stringWithUTF8String: kAFInfoDictionary_Title]];
NSLog(@"%@",Title);

NSLog(@"%@",Artist);

NSLog(@"%@",Album);

}

但是有一个问题就是,这段代码经常读取不出图片。这个问题还是交给AVURLAsset吧。

AVURLAsset *avURLAsset = [AVURLAsset URLAssetWithURL:url
options:nil];
for (NSString *format in [avURLAsset availableMetadataFormats]) {
//NSLog(@"-------format:%@",format);
for (AVMetadataItem *metadataItem in [avURLAsset metadataForFormat:format]) {
//NSLog(@"commonKey:%@",metadataItem.commonKey);
if ([metadataItem.commonKey isEqualToString:@"artwork"]) {
//取出封面artwork,从data转成image显示
MPMediaItemArtwork *mArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageWithData:[(NSDictionary*)metadataItem.value objectForKey:@"data"]]];
[dict setObject:mArt
forKey:MPMediaItemPropertyArtwork];
break;
}
}
}

很靠谱,图片获取率100%.
so, 以上是所有实现代码喽,感兴趣的自已整理吧。

参考文章:
iOS播放器特效,锁屏状态下显示专辑图片和信息
获取MP3和M4A音乐文件的歌曲信息以及专辑图片


相关博文

    分享到:

About rainbird

IOS攻城狮
This entry was posted in 默认分类 and tagged , , , , , , , , , , , . Bookmark the permalink.

8 Responses to IOS锁屏状态播放音乐时显示专辑信息和图片

  1. gerry says:

    你好,按照此方法做后,图片是出来了,但音频控制的没有出来.我也已经设置了
    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    [self becomeFirstResponder];
    请问是哪里出了问题,谢谢!

  2. peter says:

    额,请问乱码该如何解决?

  3. henry says:

    是utf8沒錯
    不過讀出來都是亂碼
    很怪
    網路上查好像是因為mp3在windows或者是mac os下編譯的關係
    請問你有這問題嗎?

  4. henry says:

    請問讀取中文歌曲資訊都是亂碼
    有辦法解決嗎?
    感謝

发表评论