Corey Zambonie
Corey Zambonie

Reputation: 614

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?

Upvotes: 5

Views: 2320

Answers (2)

Eugene Alexeev
Eugene Alexeev

Reputation: 1302

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

Upvotes: 1

ha100
ha100

Reputation: 1592

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

Upvotes: 3

Related Questions