Question

I'm writing an app, part of which allows the user stream/play videos. I want to restrict the functionality so that they can only stream videos if they have a WiFi connection. I will then save the video so that when they have a 3G only (or lesser) connection they can't stream videos and can only replay videos that are saved on the phone.

Ideally, I'd like to get MPMoviePlayerController to stream/play the movie and then access the movie data and save it. However, the MPMoviePlayerController api doesn't seem to support access to the movie data.

I'd like to avoid and download-then-play scenario. Any ideas?

Was it helpful?

Solution

Two solutions come to mind. Both this solutions require that the file is in a format that can be played progressive, e.g. that you don't need the whole file to be able to play it (but that would be a prerequisite anyway).

  1. use a thread to download the data and append it to a file, and play the file from another thread. Now, that requires that you can handle EOF events in the MPMoviePlayerController and pause the playing until the cache file is appended to and then resume for the same point.

So far what I've seen people doing this it doesn't work because MPMoviePlayerController can't handle the EOF event. (not tested it my self yet) [Caching videos to disk after successful preload by MPMoviePlayerController

  1. Skip the playing from a file and setup a local HTTP server and stream from that (on localhost). This is also not tested. The idea is that MPMoviePlayerController would handlle the event of missing data better from a HTTP stream then from reading the file directly. Downside might be that it is less efficient, but I think that is a minor increase in CPU. I don't know if the network interface would handle it, but I'm assuming it's not an issue.

I leave this answer as a wiki, because I don't have a working solution but I too want one.

OTHER TIPS

There is a way to make this work, but you have to write your own HTTP Live Streaming downloader.

Basically, you parse the .m3u8 file (it's a pretty simple standard, but can get tricky with alternate streams and the possibility that the stream will simply drop out and need a new playlist to continue) and then download the chunks in .ts format to your local storage, say the Documents folder or Caches etc.

Then you'll have to set up a local HTTP server to allow the MPMoviePlayerController or AVPlayer to access the files over HTTP (since they won't touch a local file path), including a re-coded playlist file pointing to the local files, which you'll have to create yourself from the original playlist(s). CocoaHTTPServer works great for this.

Once you've done all that, it works great. It's unavoidable that you get a little delay while you download the first chunk or two before presenting your local HTTP URL to the movie player, but after that you get seamless download, recording and preview playback.

Good luck!

the iPhone is using progressive download so it will not save on the device. For that you need to explicitly download it and then play the video from your local folder.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top