Skip to content

Instantly share code, notes, and snippets.

@AtharvaVaidya
Last active February 22, 2017 05:02
Show Gist options
  • Save AtharvaVaidya/0de719e8c9407d7ffb2d to your computer and use it in GitHub Desktop.
Save AtharvaVaidya/0de719e8c9407d7ffb2d to your computer and use it in GitHub Desktop.
//
// SMetadata.swift
// SimpleMusic
//
// Created by Atharva Vaidya on 09/08/15.
// Copyright (c) 2015 Atharva Vaidya. All rights reserved.
//
import Cocoa
import AVFoundation
class SMetadata: AVMetadataItem
{
var title: String!
var artist: String!
var album: String!
func metaDataForAVAsset(asset: AVAsset) -> [String:String]
{
//Updated code thanks to the fork from istx25 on GitHub
var metadata = ["title": "", "artist": "", "albums": ""]
let metaTitle = AVMetadataItem.metadataItemsFromArray(asset.commonMetadata, withKey: AVMetadataCommonKeyTitle, keySpace: AVMetadataKeySpaceCommon)
let titles = metaTitle.first! as! AVMetadataItem
title = "\(titles.value())"
metadata["title"] = title
let metaArtist = AVMetadataItem.metadataItemsFromArray(asset.commonMetadata, withKey: AVMetadataCommonKeyArtist, keySpace: AVMetadataKeySpaceCommon)
let artists = metaArtist.first! as! AVMetadataItem
artist = "\(artists.value())"
metadata["artist"] = artist
let metaAlbum = AVMetadataItem.metadataItemsFromArray(asset.commonMetadata, withKey: AVMetadataCommonKeyAlbumName, keySpace: AVMetadataKeySpaceCommon)
let albums = metaAlbum.first! as! AVMetadataItem
album = "\(albums.value())"
metadata["album"] = album
return metadata
}
}
@AtharvaVaidya
Copy link
Author

If you want to extract the metadata of an AVAsset, this class reduces a bit of the complexity in doing so.... or maybe it doesn't.

@karapigeon
Copy link

I thought I'd fork your SMetadata.swift gist and offer some insight/improvements! https://gist.github.com/istx25/41222e49437ba259af05

Few things to keep in mind when writing Swift:

  • Always use let when Xcode doesn't complain.
  • Use Swifty methods like metaTitle.first! instead of metaTitle[0].
  • If you're providing your initialised data types data, in the same place: do it in one line!

Happy coding! 😺

@AtharvaVaidya
Copy link
Author

Thanks a lot for the advice, I'm still a beginner.

The strange thing is I already knew points 1 and 3, but for some reason my brain didn't think to write the code in that way. Oh well, now I'll remember to do those things because they'll stick in my memory.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment