1
votes

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.

1

1 Answers

0
votes

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