7
我正在使用雲存儲的ios音樂播放器。我如何從ios開發中的mp3文件中提取元數據
我需要提取音樂信息,如標題,藝術家,藝術品。
我有一個叫做playit的動作,可以播放和暫停mp3文件。它還應該使用與mp3文件關聯的metadtaa來填充一些UILables和UIImage。問題是我無法從超過25個不同的mp3文件中提取元數據。這是我的代碼:
文件的URL是正確的,因爲音頻播放器能夠找到並播放它,但我不知道爲什麼avmetadataitem無法獲取元數據。
- (IBAction)playIt:(id)sender {
AVAudioPlayer *audioPlayer;
AVAsset *assest;
NSString * applicationPath = [[NSBundle mainBundle] resourcePath];
NSString *secondParentPath = [applicationPath stringByDeletingLastPathComponent];
NSString *soundFilePath = [[secondParentPath stringByAppendingPathComponent:@"fisal1407"] stringByAppendingPathComponent:[musicFiles objectForKey:@"show_id"] ];
NSURL *fileURL = [NSURL URLWithString:[soundFilePath stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
assest = [AVURLAsset URLAssetWithURL:fileURL options:nil];
NSArray *metadata = [assest commonMetadata];
for (NSString *format in metadata) {
for (AVMetadataItem *item in [assest metadataForFormat:format]) {
if ([[item commonKey] isEqualToString:@"title"]) {
filename.text = (NSString *)[item value];
NSLog(@" title : %@", (NSString *)[item value]);
}
if ([[item commonKey] isEqualToString:@"artist"]) {
show_id.text = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:@"albumName"]) {
// _albumName = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:@"artwork"]) {
NSData *data = [(NSDictionary *)[item value] objectForKey:@"data"];
UIImage *img = [UIImage imageWithData:data] ;
imageView.image = img;
continue;
}
}
}
if (audioPlayer == nil) {
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:&error];
audioPlayer.numberOfLoops = -1;
[audioPlayer play];
[sender setImage:[UIImage imageNamed:@"player_044.gif"] forState:UIControlStateNormal];
}
else
{
if (audioPlayer.isPlaying)
{
[sender setImage:[UIImage imageNamed:@"player_04.gif"] forState:UIControlStateNormal];
[audioPlayer pause];
} else {
[sender setImage:[UIImage imageNamed:@"player_044.gif"] forState:UIControlStateNormal];
[audioPlayer play];
}
}
}
集成代碼(在下面的答案Alex的建議的修改)與代碼在一起,我發現從M4A提取產生更普遍功能。你可以在包含我使用的m4a代碼的問題的答案中找到它:http://stackoverflow.com/questions/14030746/ios-avfoundation-how-do-i-fetch-artwork-from-an-mp3-文件/ 14046486#14046486 - 感謝所有參與! –