gpt4 book ai didi

objective-c - 如何在 ios 开发中从 mp3 文件中提取元数据

转载 作者:可可西里 更新时间:2023-11-01 03:33:14 24 4
gpt4 key购买 nike

我正在开发带有云存储的 iOS 音乐播放器。

我需要提取音乐信息,如标题、艺术家、艺术作品。

我有一个名为 playit 的 Action ,它播放和暂停 mp3 文件。它还应该使用与 mp3 文件关联的 metadtaa 填充一些 UILables 和 UIImage。问题是我无法从超过 25 个不同的 mp3 文件中提取元数据。这是我的代码:

文件url是正确的,因为音频播放器能够找到并播放它,但我不知道为什么avmetadatataitem无法获取元数据。

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

最佳答案

尝试

for (NSString *format in [asset availableMetadataFormats])

代替

NSArray *metadata = [assest commonMetadata];
for (NSString *format in metadata) {

关于objective-c - 如何在 ios 开发中从 mp3 文件中提取元数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12884067/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com