I'm currently trying to play a video to a URL that has the custom scheme defined in a custom NSURLProtocol subclass. Initially I was using MPMoviePlayerController in an attempt to accomplish this, but after running into problems and checking stack overflow, I found that the MPMoviePlayerController does not handle NSURLProtocol subclasses as expected.
How to play movie with a URL using a custom NSURLProtocol?
As a result I decided to look at the AVFoundation framework, however, it seems that this also doesn't seem to work. I just wanted to know if this was possible, or am I trying to walk through walls?
Using AVFoundation, the approach I'm using is shown below. It's probably worth mentioning that this works when using a standard URL to a video hosted on the internet, but doesn't work with the custom NSURLProtocol.
// this doesn't work
//AVPlayer *player = [[AVPlayer alloc] initWithURL:[NSURL urlWithString:@"custom URL scheme"]];
// this works
AVPlayer *player = [[AVPlayer alloc] initWithURL:[NSURL urlWithString:@"some url to video on remote server"]];
AVPlayerLayer *layer = [AVAVPlayerLayer playerLayerWithPlayer:player];
// configure the layer
[self.view.layer addSublayer:layer];
[player play];
Is there something different that would need to be done in order to play from the defined NSURLProtocol subclass?
Recently, I managed to get NSURLProtocol to work with MPMoviePlayerController. This is mainly useful because MPMoviePlayerController only accepts an NSURL so it didn't let us pass cookies or headers (for authentication). The use of NSURLProtocol allowed that. I thought I'd share some code here for the community for once.
Basically, by using NSURLProtocol, we can intercept the communication between MPMoviePlayerController and the streamed request, to inject cookies along the way, or possibly save the stream offline, or replace it with cat videos ect... To do this, you need to create a new class My extending NSURLProtocol:
MyURLProtocol.h:
MyURLProtocol.m:
usage: