1

I am attempting to play a video by using a data URI (data:video/mp4;base64,AAAAHGZ0eXBtcDQyAAAAAG1wNDJpc29......). Here is my code thus far:

func videoDataWasLoaded(data: NSData) {
    let moviePlayer = MPMoviePlayerController()
    let base64 = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions(rawValue: 0))
    let dataUri = NSURL(string: "data:video/mp4;base64,\(base64)")
    moviePlayer.contentURL = dataUri
    moviePlayer.play()
}

I have confirmed that the video plays by writing the data (NSData) to a tmp file and then using that for the contentURL. However, writing to disk is slow, and I figured that the data URI approach would be faster especially since my movie files are small (around 5 seconds each).

UPDATE: This question is not so much concerned about which method (AVPlayer, MPMoviePlayerController) is used to play the video. Rather, it is concerned with the possibility of playing a video from a data URI. Here is a link which describes what I am wanting to do in terms of HTML5.

jfizz
  • 465
  • 5
  • 17

1 Answers1

0

This code plays a movie from a URL ... assuming that is your question?

let videoURL = self.currentSlide.aURL
self.playerItem = AVPlayerItem(URL: videoURL)
self.player = AVPlayer(playerItem: self.playerItem)
self.playerLayer = AVPlayerLayer(player: self.player)
self.streamPlayer =  AVPlayerViewController()
self.streamPlayer.player = self.player
self.streamPlayer.view.frame = CGRect(x: 128, y: 222, width: 512, height: 256)
self.presentViewController(self.streamPlayer, animated: true) {
        self.streamPlayer.player!.play()
}

But sorry, that is to play a URL; you want an URI. I though you had mis-typed your question, my error. I looked up URI this time :|

The answer must surely lie in the conversion of your video source to a playable stream, as in an M3u8. Here is an excellent post on the subject it seems.

http://stackoverflow.com/questions/6592485/http-live-streaming
user3069232
  • 8,587
  • 7
  • 46
  • 87
  • Thanks for the answer. However, I was wanting to know if I can play a video using a data URI (basically a base64 encoded string) as opposed to writing the file to disk. – jfizz Feb 28 '16 at 16:33
  • I believe this code is streaming the file, it is not downloading it first. – user3069232 Feb 28 '16 at 18:19
  • That sounds like it could possibly be a solution. Would you mind linking to the appropriate documentation where it details the streaming? – jfizz Mar 01 '16 at 04:32
  • I watched a tech talk yesterday on AVPlayer class; it was/is focused on the AppleTV, but was given in January 2015, so is bang up-to-date. https://developer.apple.com/videos/play/techtalks-apple-tv/6/ – user3069232 Mar 01 '16 at 08:08
  • You might also want to watch the tech talk WWDC 2013 which talks about the AV foundation and avkit https://developer.apple.com/videos/play/wwdc2013/606/ – user3069232 Mar 01 '16 at 08:15
  • I appreciate the answer and the helpful links. This could possibly serve as an alternative solution, however, I am going to leave the question unanswered as your solution does not address playing a video from a data URI. I have updated the question to clarify what I am looking for. – jfizz Mar 02 '16 at 02:38
  • jfizz; I have updated the answer with a link to a means to convert your data stream into an m3u8 which is the format the AVPlayerController is looking for. The other route, most browser can do this; so go and look at the source code of CHROME https://www.chromium.org for example and perhaps you can find the code it uses; although a real challenge – user3069232 Mar 02 '16 at 07:51
  • The above answer has nothing to do with the question. I actually just attempted to get this data: type of NSURL working with a video file and it does not seem to be possible using this API. Another possible approach would be to make use of NSURLProtocol: https://www.raywenderlich.com/2292-using-nsurlprotocol-with-swift – MoDJ May 27 '19 at 17:38