实际上,我正在用这个简单的代码部分来播放一首歌:
NSURL *url=[NSURL fileURLWithPath:clickedPath];
AVPlayer *player = [AVPlayer playerWithURL:url];
AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
[self presentViewController:controller animated:YES completion:nil];
controller.player = player;
[player play];
我想做的是使用 AVPlayerViewController 的 contentOverlayView 属性显示的自定义 View 来显示歌曲的信息(艺术家、专辑名称和插图等)。 我在这里到处搜索,但没有发现任何对我的情况有用的东西(找到 MPMediaPlayer 解决方案或不起作用的解决方案)。
你能帮我一下吗?我愿意测试您的建议并进行讨论!
提前致谢
最佳答案
NSString *path = [[NSBundle mainBundle] pathForResource:@"youraudiomp3filename" ofType:@"mp3"];
NSURL *url=[NSURL fileURLWithPath:path];
AVPlayer *player = [AVPlayer playerWithURL:url];
AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];
NSArray *metadata = [asset commonMetadata];
NSString *title;
NSString *artist;
NSString *albumName;
UIImageView *imageView;
for (AVMetadataItem *item in metadata) {
if ([[item commonKey] isEqualToString:@"title"]) {
title = (NSString *)[item value];
}
if ([[item commonKey] isEqualToString:@"artist"]) {
artist = (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;
}
}
然后您可以将 Assets 设置为您的 AVPlayerViewController 的播放器,播放并呈现。
关于ios - 使用他的路径获取音频文件的信息 - Objective-c iOS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37388867/