Synny
Synny

Reputation: 542

Get info of audio file with his path - Objective-c iOS

Actually, I'm playing a song with this simple part of code:

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

What I would like to do is showing the song's info (artist, album name and artwork, etc...) using a custom view showed with contentOverlayView's property of AVPlayerViewController. I've searched all around here but found nothing that would be useful for my case (either found MPMediaPlayer solution or not working ones).

Coule you help me please ? I'm open to test your suggestions and discuss about it !

Thanks in advance

Upvotes: 0

Views: 1422

Answers (2)

Synny
Synny

Reputation: 542

Thanks to the @L A's help, I've successfully managed to get all the song's info with:

[[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil];


        NSURL *url=[NSURL fileURLWithPath:clickedPath];
        NSLog(@"url : %@", url);
        avPlayer = [AVPlayer playerWithURL:url];


        AVAsset *asset = [AVURLAsset URLAssetWithURL:url options:nil];

        NSArray *metadata = [asset commonMetadata];
        NSString *songTitle;
        NSString *artist;
        NSString *albumName;
        UIImageView *albumImageView = [[UIImageView alloc] init];


        for (AVMetadataItem *item in metadata) {

            if ([[item commonKey] isEqualToString:@"title"]) {

                songTitle = (NSString *)[item value];
                NSLog(@"song title : %@", songTitle);

            }

            if ([[item commonKey] isEqualToString:@"albumName"]) {

                albumName = (NSString *)[item value];
                NSLog(@"album name : %@", albumName);

            }

            if ([[item commonKey] isEqualToString:@"artist"]) {

                artist = (NSString *)[item value];
                NSLog(@"song artist : %@", artist);

            }

            if ([[item commonKey] isEqualToString:@"artwork"]) {

                NSData *data = [item dataValue];
                UIImage *img = [UIImage imageWithData:data] ;
                albumImageView.image = img;
                [albumImageView setTranslatesAutoresizingMaskIntoConstraints:NO];
                continue;

            }
        }


        AVPlayerViewController *controller = [[AVPlayerViewController alloc] init];
        [self presentViewController:controller animated:YES completion:nil];
        [controller.contentOverlayView addSubview:albumImageView];
        controller.player = avPlayer;
        [avPlayer play];

Upvotes: 0

L A
L A

Reputation: 976

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

then you can set the asset to the player of your AVPlayerViewController, play and present.

Upvotes: 1

Related Questions