How to play local M3u8 files on iOS via GCDWebServer

I am trying to run a local GCDWebServer to work with an M3U8 file that I saved locally from the server. I parsed the file and saved each .ts file to local storage. Now I am trying to serve this file through a local web server, but I can not get the file to play using 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 reproduce the code:

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

Is there something that I am doing wrong in the way I maintain the local m3u8 file? Also, does the local web server work in a safe way to host content?

+5
source share
2 answers

change the content type to application /vnd.apple.mpegurl as specified in RFC section 3.1

+2
source
 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:

Using this setting of the web server, a request to the server address will return a list of files in the folder specified in directoryPath . Thus, the link to start the HLS stream will look like http://server_local_address/playlist.m3u8

+1
source

Source: https://habr.com/ru/post/1237353/


All Articles