Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Load NSData into an AVPlayerItem?

This is a follow-on question from my post about How to Encrypt mp3 Files in iOS.

How would I go about loading a track into an AVPlayerItem using NSData rather than a file path/URL?

Thanks

like image 339
Kiran Panesar Avatar asked Aug 02 '12 15:08

Kiran Panesar


2 Answers

[/*name of NSData*/ writeToFile:/*save file path*/ atomically: YES];
NSURL *filepath = [NSURL fileURLWithPath:/*save file path*/];
AVPlayerItem *player = [AVPlayerItem playerItemWithURL:filepath];

Untested, but should work.

like image 166
Dustin Avatar answered Oct 03 '22 00:10

Dustin


Updating Dustin's answer for Swift 5:

var audioData: Data // some audio data
var fileURL: URL // some local path, probably appending NSTemporaryDirectory() 
try! audioData.write(to: fileURL)
let item = AVPlayerItem(url: fileURL)
like image 34
followben Avatar answered Oct 03 '22 02:10

followben