5

Can anyone guide me, how to store locally m3u8 file from url, for playing offline later. Will EXT-X-ALLOW-CACHE help me in this matter.`

NSURLRequest* request = [NSURLRequest requestWithURL:url   cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
NSURLConnection *connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];
NSLog(@"connection %@",connection);`

I can stream & play m3u8 directly from url. But not able to store it locally and parse .ts files and play...Pls guide.

Daisy
  • 71
  • 1
  • 5

1 Answers1

-2

Step 1: Download the file & save

NSError *err = nil;
NSString *url = [[NSString stringWithFormat:@"http://www.example.com/playlist.m3u8"] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *the_file = [NSString stringWithContentsOfURL:[NSURL URLWithString:url] encoding:NSUTF8StringEncoding error:&err];
if(err != nil) {
    // do some error handling, e.g. NSLog(@"Fail: %@", [err localizedDescription]);
} else {
    // save the file ( see Step 2 )
}

Step 2 : Save the file

You can either save it as file, or save it as an object .

// save as file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [paths objectAtIndex:0];
NSString *fileName = [NSString stringWithFormat:@"%@/playlist.m3u8", docDirectory];
[the_file writeToFile:fileName atomically:NO encoding:NSASCIIStringEncoding error:nil];

// OR, save as object
[[NSUserDefaults standardUserDefaults] setObject:the_file forKey:@"MY_M3U8"];

Step 3 : Read the file

// read from file 
NSString *textPath = [docDirectory stringByAppendingPathComponent:@"playlist.m3u8"];
// the FILE is loaded


// OR, read from object
NSString *content = [[NSUserDefaults standardUserDefaults] stringForKey:@"MY_M3U8"];
// it will contain the CONTENT of file

Step 4 : Play it in your player

You should have done it. No need for me to write, yes?

Raptor
  • 53,206
  • 45
  • 230
  • 366
  • I could save the ts files but not able to play it.It is in encrypted form. and probably i am not getting the decryption key. For this do i need to have EXT-X-ALLOW-CACHE= Yes. Pls suggest – Daisy Apr 15 '13 at 12:04
  • you didn't mention it is decrypted. To decrypt, we have to know how to encrypt. – Raptor Apr 15 '13 at 12:06
  • 2
    @Raptor, are you sure you can play .m3u8 files which are stored locally? As far as I can tell, the player does not support that - the file needs to be downloaded via HTTP (yes, I know it's dumb). Here are a couple of threads where people also discovered it: 1) http://stackoverflow.com/questions/3199232/mpmovieplayercontroller-and-local-m3u8-files 2) http://stackoverflow.com/questions/15427360/how-to-play-m3u8-or-ts-file-in-ios – Lukasz Czerwinski Jul 11 '15 at 04:46
  • @LukaszCzerwinski thanks for pointing out the contents of `.m3u8` have to be a valid URL instead of a local file URL. Hosting a local web server in App is one solution, while putting the media online is a much easier way. Alternatively, instead of using `.m3u8`, it is possible to make a `NSArray` / `NSDictionary` playlist to store all media local URL as well, depends on the need. – Raptor Jul 13 '15 at 02:36
  • @Raptor Thanks. Are you sure NSArray or NSDictionary can be used to store a list of .ts files? `.m3u8` file seems to be a part of HLS protocol, so I'm not sure if it can be bypassed. – Lukasz Czerwinski Jul 13 '15 at 21:57
  • While the question did ask how to download a `.m3u8` file, it was asking how to play video from it. –  Aug 06 '15 at 15:11
  • @Rollo read carefully before you downvote. OP is asking "how to save it locally". – Raptor Aug 07 '15 at 01:55
  • It's implied that he wants it for playing offline later. It would be worth explaining that saving a `.m3u8` file wont help him. –  Aug 07 '15 at 08:45