objective-c – iOS AVFoundation:如何从mp3文件中获取图稿?
发布时间:2020-12-16 09:13:57 所属栏目:百科 来源:网络整理
导读:我的代码: - (void)metadata {AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil];NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKe
我的代码:
- (void)metadata { AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil]; NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon]; NSArray *titles = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon]; NSArray *artists = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtist keySpace:AVMetadataKeySpaceCommon]; NSArray *albumNames = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyAlbumName keySpace:AVMetadataKeySpaceCommon]; AVMetadataItem *artwork = [artworks objectAtIndex:0]; AVMetadataItem *title = [titles objectAtIndex:0]; AVMetadataItem *artist = [artists objectAtIndex:0]; AVMetadataItem *albumName = [albumNames objectAtIndex:0]; if ([artwork.keySpace isEqualToString:AVMetadataKeySpaceID3]) { NSDictionary *dictionary = [artwork.value copyWithZone:nil]; self.currentSongArtwork = [UIImage imageWithData:[dictionary objectForKey:@"data"]]; } else if ([artwork.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) { self.currentSongArtwork = [UIImage imageWithData:[artwork.value copyWithZone:nil]]; } self.currentSongTitle = [title.value copyWithZone:nil]; self.currentSongArtist = [artist.value copyWithZone:nil]; self.currentSongAlbumName = [albumName.value copyWithZone:nil]; self.currentSongDuration = self.audioPlayer.duration; } 这适用于从m4a文件中获取图稿,但不适用于mp3文件.如果资产指向mp3文件,则艺术品为空.我做错了什么,我该如何解决? 解决方法
我发现在同步设置图像时,艺术品是异步加载的.我这样解决了:
- (void)metadata { AVURLAsset *asset = [AVURLAsset URLAssetWithURL:self.fileURL options:nil]; NSArray *titles = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyTitle keySpace:AVMetadataKeySpaceCommon]; NSArray *artists = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtist keySpace:AVMetadataKeySpaceCommon]; NSArray *albumNames = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyAlbumName keySpace:AVMetadataKeySpaceCommon]; AVMetadataItem *title = [titles objectAtIndex:0]; AVMetadataItem *artist = [artists objectAtIndex:0]; AVMetadataItem *albumName = [albumNames objectAtIndex:0]; NSArray *keys = [NSArray arrayWithObjects:@"commonMetadata",nil]; [asset loadValuesAsynchronouslyForKeys:keys completionHandler:^{ NSArray *artworks = [AVMetadataItem metadataItemsFromArray:asset.commonMetadata withKey:AVMetadataCommonKeyArtwork keySpace:AVMetadataKeySpaceCommon]; for (AVMetadataItem *item in artworks) { if ([item.keySpace isEqualToString:AVMetadataKeySpaceID3]) { NSDictionary *d = [item.value copyWithZone:nil]; self.currentSongArtwork = [UIImage imageWithData:[d objectForKey:@"data"]]; } else if ([item.keySpace isEqualToString:AVMetadataKeySpaceiTunes]) { self.currentSongArtwork = [UIImage imageWithData:[item.value copyWithZone:nil]]; } } }]; self.currentSongTitle = [title.value copyWithZone:nil]; self.currentSongArtist = [artist.value copyWithZone:nil]; self.currentSongAlbumName = [albumName.value copyWithZone:nil]; self.currentSongDuration = self.audioPlayer.duration; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |