AVFoundation pour iOS: Comment récupérer des illustrations à partir d'un file mp3?

Mon code:

- (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 isEqualToSsortingng:AVMetadataKeySpaceID3]) { NSDictionary *dictionary = [artwork.value copyWithZone:nil]; self.currentSongArtwork = [UIImage imageWithData:[dictionary objectForKey:@"data"]]; } else if ([artwork.keySpace isEqualToSsortingng: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; } 

Cela fonctionne pour récupérer des illustrations à partir de files m4a, mais ne fonctionne pas pour les files mp3. Si l' asset pointe vers un file mp3, les artworks sont vides. Qu'est-ce que je fais mal et comment puis-je le réparer?

J'ai trouvé que les œuvres étaient chargées de manière asynchronous alors que l'image était réglée de manière synchrone. Je l'ai résolu de cette façon:

 - (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 isEqualToSsortingng:AVMetadataKeySpaceID3]) { NSDictionary *d = [item.value copyWithZone:nil]; self.currentSongArtwork = [UIImage imageWithData:[d objectForKey:@"data"]]; } else if ([item.keySpace isEqualToSsortingng: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; } 

Je viens de find la réponse à cette question: Comment puis-je extraire les métadonnées d'un file mp3 dans le développement d'ios et cherchait maintenant à voir pourquoi ce code ne fonctionne pas sur un file m4a. J'ai pris un peu de votre code et présente ce qui suit qui semble fonctionner à la fois pour m4a et mp3. Notez que j'ai laissé le code qui fonctionne pour mp3 comme une clause else ouverte sans le qualificatif – si quelqu'un gagne plus d'expérience avec ceci, ils sont invités à affiner!

  - (void)getMetaDataForSong:(MusicItem *)musicItem { AVAsset *assest; // filePath looks something like this: /var/mobile/Applications/741647B1-1341-4203-8CFA-9D0C555D670A/Library/Caches/All Summer Long.m4a NSURL *fileURL = [NSURL fileURLWithPath:musicItem.filePath]; NSLog(@"%@", fileURL); assest = [AVURLAsset URLAssetWithURL:fileURL options:nil]; NSLog(@"%@", assest); for (NSSsortingng *format in [assest availableMetadataFormats]) { for (AVMetadataItem *item in [assest metadataForFormat:format]) { if ([[item commonKey] isEqualToSsortingng:@"title"]) { musicItem.strSongTitle = (NSSsortingng *)[item value]; } if ([[item commonKey] isEqualToSsortingng:@"artist"]) { musicItem.strArtistName = (NSSsortingng *)[item value]; } if ([[item commonKey] isEqualToSsortingng:@"albumName"]) { musicItem.strAlbumName = (NSSsortingng *)[item value]; } if ([[item commonKey] isEqualToSsortingng:@"artwork"]) { UIImage *img = nil; if ([item.keySpace isEqualToSsortingng:AVMetadataKeySpaceiTunes]) { img = [UIImage imageWithData:[item.value copyWithZone:nil]]; } else { // if ([item.keySpace isEqualToSsortingng:AVMetadataKeySpaceID3]) { NSData *data = [(NSDictionary *)[item value] objectForKey:@"data"]; img = [UIImage imageWithData:data] ; } musicItem.imgArtwork = img; } } } }