Search code examples
ioshttp-live-streaming

HTTP Live Streaming -Play from Bundle


Similar questions have been asked before, but I could never find a solution.

I am trying to play a m3u8 file from bundle . I have streamed it successfully over http .but i really need to play movie from bundle and wanted to use http live streaming .


Solution

  • 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