ios - Can't retrieve non jpg/png files from the Asset Catalog in Xcode 7 -
i want retrieve non jpg/png files asset catalog in xcode 7. saw possible add kind of data asset catalog.
so way doing working before moving gif/video xcode project assets catalog
private struct walkthroughvideo { static let name = "tokyo" static let type = "mp4" } private var movieplayer : avplayerviewcontroller? = { guard let path = nsbundle.mainbundle().pathforresource(walkthroughvideo.name, oftype: walkthroughvideo.type) else { return nil } let url = nsurl.fileurlwithpath(path) let playerviewcontroller = avplayerviewcontroller() playerviewcontroller.player = avplayer(url: url) return playerviewcontroller }()
so path nil. can't find file. there new way retrieve dataset ?
obviously pathforresource
not going work, because resource not in app bundle more - it's in asset catalog. in effect, no longer has "path".
you have use new nsdataasset class, can fetch arbitrary data name out of asset catalog. simulate tokyo.mp4 example, used aiff sound file, stored in asset catalog data set called theme (in universal slot). here's complete test code (in root view controller):
var player : avaudioplayer! override func viewdidload() { super.viewdidload() if let asset = nsdataasset(name: "theme") { let data = asset.data self.player = try! avaudioplayer(data:data) self.player.play() } }
lo , behold, sound played! of course in real life have added error handling on try
.
Comments
Post a Comment