5

I'm attempting to run a local GCDWebServer to serve up an M3U8 file I have stored locally from a server. I parsed the file and saved each .ts file to local storage. Now I'm trying to serve that file up through a local web server, but I'm unable to get the file to play using either MPMoviePlayerController or AVPlayerViewController.

Here is my server code:

webServer = [[GCDWebServer alloc] init];

[webServer addDefaultHandlerForMethod:@"GET"
                         requestClass:[GCDWebServerRequest class]
                         processBlock:^GCDWebServerResponse *(GCDWebServerRequest* request) {

                             NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
                             NSString *docDirectory = [paths objectAtIndex:0];
                             NSString *textPath = [docDirectory stringByAppendingPathComponent:@"localPlaylist.m3u8"];
                             return [GCDWebServerDataResponse responseWithData:[NSData dataWithContentsOfFile:textPath] contentType:@".m3u8"];
                         }];

[webServer startWithPort:8080 bonjourName:nil];

and my subsequent attempt to play the code:

 AVPlayerViewController *newPlayer = [[AVPlayerViewController alloc] init];
newPlayer.player = [[AVPlayer alloc]initWithURL:webServer.serverURL];
[self presentViewController:newPlayer animated:YES completion:nil];

Is there anything I'm doing wrong in the way I'm serving up the local m3u8 file? Also, is running a local web server a secure way to host content?

Corey Zambonie
  • 614
  • 5
  • 17
  • Have you got any success to play locally saved m3u8 file and encrypted/decrypted chunks? I am currently working on same flow, need some guidance. – Rahul Verma Jul 25 '17 at 09:00

2 Answers2

3

change your content-type to application/vnd.apple.mpegurl as stated in RFC section 3.1

Community
  • 1
  • 1
ha100
  • 1,563
  • 1
  • 21
  • 28
1
NSString *somePath = @"path/to/folder/with/your/playlist";

GCDWebServer *webServer = [[GCDWebServer alloc] init];
[webServer addGETHandlerForBasePath:@"/" directoryPath:somePath indexFilename:nil cacheAge:3600 allowRangeRequests:YES];
[webServer start];

Edit:

With this setup of webserver, request to the server address will return list of files of folder which was pointed out in directoryPath. So link for launching HLS stream will look like http://server_local_address/playlist.m3u8

Eugene Alexeev
  • 1,152
  • 12
  • 32