如何从mp3文件中提取图稿,实现iOS AVFoundation功能?

2026-04-16 22:183阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计367个文字,预计阅读时间需要2分钟。

如何从mp3文件中提取图稿,实现iOS AVFoundation功能?

我的代码:使用AVURLAsset和URLAsset获取文件URL,并从中提取艺术作品信息。

我的代码:

- (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文件,则艺术品为空.我做错了什么,我该如何解决?

我发现在同步设置图像时,艺术品是异步加载的.我这样解决了:

如何从mp3文件中提取图稿,实现iOS AVFoundation功能?

- (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; }

本文共计367个文字,预计阅读时间需要2分钟。

如何从mp3文件中提取图稿,实现iOS AVFoundation功能?

我的代码:使用AVURLAsset和URLAsset获取文件URL,并从中提取艺术作品信息。

我的代码:

- (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文件,则艺术品为空.我做错了什么,我该如何解决?

我发现在同步设置图像时,艺术品是异步加载的.我这样解决了:

如何从mp3文件中提取图稿,实现iOS AVFoundation功能?

- (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; }