Yuri
Yuri

Reputation: 13

How to get ID3 tags for media files on iPhone

I need some help. The situation looks like this: I can get list of media files (mp3s, m4as, m4vs an so on) with link to the file (looks like:

http://10.0.1.1/Media/Files%20Folder/File%20Itself.m4v

So I get an array consisting of links to these files.

I need to display these items in UITableView with corresponding tags (Genre, Artist etc.) and most importantly, album art (it's embedded in file).

How can I fetch that information? If possible, without loading whole media file. Thanks for any help.

Upvotes: 0

Views: 1806

Answers (3)

Eric
Eric

Reputation: 16921

Check out the AVMetadataItem API:

let asset = AVAsset(url: url)

assert(asset.isReadable)

let artwork = AVMetadataItem.metadataItems(from: asset.metadata, filteredByIdentifier: .commonIdentifierArtwork).first?.dataValue
let artist = AVMetadataItem.metadataItems(from: asset.metadata, filteredByIdentifier: .commonIdentifierArtist).first?.stringValue
let title = AVMetadataItem.metadataItems(from: asset.metadata, filteredByIdentifier: .commonIdentifierTitle).first?.stringValue
let albumName = AVMetadataItem.metadataItems(from: asset.metadata, filteredByIdentifier: .commonIdentifierAlbumName).first?.stringValue
let duration = asset.duration.seconds

Upvotes: 0

Nathaniel S.
Nathaniel S.

Reputation: 11

I wrote a class for this

It's called metadataRetriever. It returns an array of the

  • artist,
  • song,
  • and album

as NSStrings (in that order)

It's really fast and is safe for synchronous use.

One caveat, it doesn't work for .m4a's because the id3 info for .m4a's is different.

Upvotes: 1

jtbandes
jtbandes

Reputation: 118691

You'll want to use AVURLAsset along with the method -loadValuesAsynchronouslyForKeys:completionHandler:.

Also note that the docs say

AVAsset and other classes provide their metadata “lazily” (see AVAsynchronousKeyValueLoading), meaning that you can obtain objects from those arrays without incurring overhead for items you don’t ultimately inspect.

So I'm guessing you won't run into problems of loading the whole file.

Upvotes: 0

Related Questions