ios - 使用他的路径获取音频文件的信息 - Objective-c iOS

标签 ios objective-c audio avplayer

实际上,我正在用这个简单的代码部分来播放一首歌:

    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/

相关文章:

ios - 如何在 stringWithContentsOfURL :encoding:error:? 中限制字符串的大小

iphone - iPhone-尝试制作纯色UIToolbar

ios - Pod 安装停留在 "Setting up CocoaPods Master repo"

javascript - 如何使用CheckBox或将按钮从一个 Activity 切换到另一个 Activity ,在android中打开或关闭一个特定的音频文件?

ios - 解析两个键等于一个值的查询

objective-c - 按下主页按钮时进行服务器调用

ios - NSTimer 在后台不工作

objective-c - iOS 开发 - 如何从自定义 View Controller 类接收按钮事件?

android - 取消注册BroadCast onDestroy()

flash - Actionscript 3.0麦克风分贝