2011-09-01 81 views
3

我想獲得專輯圖像/歌曲封面。可能嗎?我使用AVPlayer進行音頻流傳輸,如果歌曲包含它,我需要獲取專輯封面。就像在iTunes中一樣。AVFoundation獲取歌曲的專輯圖像

SOLUTION:

UIImage *img = nil; 
NSArray *metadata = _avplayer.currentItem.asset.commonMetadata; 
for(AVMetadataItem *item in metadata){ 
    if([item.commonKey isEqualToString:@"artwork"]){ 
     NSData *data = item.value[@"data"]; 
     img = [UIImage imageWithData:data]; 
     return img; 
    } 
} 
return img; 
+0

你好,你有沒有找到如何獲得專輯封面?我嘗試了用戶最後的FM服務,但是我的收音機中的歌手並不流行,所以我無法獲得專輯封面(( –

回答

0

前提是你的文件是正確的格式,即MP3,你可以將文件的數據加載到ID3標籤解析器。 Here's an example of one.它是爲OS X製作的,但它看起來應該很容易移植到iOS。

1

你應該試試這個,但我用MPMusicPlayer:

musicPlayer = [MPMusicPlayerController iPodMusicPlayer]; 
currentItem = [musicPlayer nowPlayingItem]; 
if (currentItem == nil) { 
    //Not playing 
} 
else 
{ 
    NSString *name = [currentItem valueForProperty:@"title"]; 
    int length = [name length]; 
    if (length > 30) { 
     name = [name substringWithRange:NSMakeRange(0, 30)]; 
     name = [name stringByAppendingString:@"..."]; 
    } 
    NSString *album = [currentItem valueForProperty:@"albumTitle"]; 
    int lengthAl = [album length]; 
    if (lengthAl > 30) { 
     album = [album substringWithRange:NSMakeRange(0, 30)]; 
     album = [album stringByAppendingString:@"..."]; 
    } 
    NSString *artist = [currentItem valueForProperty:@"artist"]; 
    int lengthAr = [artist length]; 
    if (lengthAr > 30) { 
     artist = [artist substringWithRange:NSMakeRange(0, 30)]; 
     artist = [artist stringByAppendingString:@"..."]; 
    } 
    MPMediaItemArtwork *artworkPrev = [currentItem valueForProperty:@"artwork"]; 
    if (artworkPrev != nil) 
    { 
     UIImage *img = [artworkPrev imageWithSize:CGSizeMake(65, 65)]; 
     UIImageView *imgNew = [[UIImageView alloc] initWithImage:img]; 
     imgNew.frame = CGRectMake(-15,-20, 65, 65); 
    } 
} 

正如你可以看到我使用NSMakeRange,我用它只是在我的應用程序不會留下空間。我希望能幫到你

+1

)這隻適用於本地播放,我需要在流媒體時播放圖片:)但是,謝謝,這將在某一天有用 –

+0

從什麼服務流?:P –

+0

還需要流媒體音樂專輯封面! –

相關問題