Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to play local M3u8 files on iOS through GCDWebServer

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?

like image 328
Corey Zambonie Avatar asked Sep 26 '22 01:09

Corey Zambonie


2 Answers

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

like image 133
ha100 Avatar answered Oct 03 '22 21:10

ha100


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

like image 22
Eugene Alexeev Avatar answered Oct 03 '22 22:10

Eugene Alexeev