How do I use iOS for live, on-demand, and cache of videos? -http Live Streaming (HLS)

Source: Internet
Author: User

This article "How to use iOS to achieve the 芈 month" live, on-demand? -http Live Streaming (HLS) (i) "

Hls-demo Address: Https://github.com/yangchao0033/HLS-Demo

Please note the following questions before using the demo, I also made comments in the code.

#warning be careful not to use the primary index of the switch stream directly, The protocol of the current code only provides for the download and playback of the sub-index of. TS positioning, and only on-demand protocol that a small segment is downloadable, the live protocol can only play, unable to download, because the m3u8 library only for a particular kind of m3u8 format has been parsed, and m3u8 format there are many kinds, so can't hike out , the demo is only a demo, not all the format of full parsing, if you are interested in the M3U8 library can be extended, pull request on GitHub I do a supplemental extension, I will be in time to update the blog. Blog address: Superyang.gitcafe.io or Yangchao0033.github.io with Pinterest: Http://www.jianshu.com/users/f37a8f0ba6f8/latest_articles/** on-demand protocol (only this can be downloaded, but too short, no way to play out, is looking to download and play a new on-demand or live feed, hope that readers can help to provide ha, not very grateful ~) *//#define TEST_HLS_URL @ " http://m3u8.tdimg.com/147/806/921/3.m3u8 "/** video Live protocol *//** Parent index (cannot download, only as structural analysis) *//#define TEST_HLS_URL @" Http://dlhls.cdn.zhanqi.tv/zqlive/34338_PVMT5.m3u8 "/** sub-index (cannot download, only as structural analysis) *//#define TEST_HLS_URL @" http:/ /dlhls.cdn.zhanqi.tv/zqlive/34338_pvmt5_1024/index.m3u8? Dnion_vsnae=34338_pvmt5 "/** WWCD Video, sure enough Apple itself to use this protocol (cannot download, only as a structural analysis) *//#define TEST_HLS_URL @" HTTP/ devstreaming.apple.com/videos/wwdc/2015/413eflf3lrh1tyo/413/hls_vod_mvp.m3u8 "      

If you find the article useful, ask the reader to point to a star on GitHub, or to have a good view of the book.

This article is also published on my blog , welcome you to leave a message.
Blog Address: http://superyang.gitcafe.io/blog/2016/02/14/hls-2/

Demo Configuration principle:

1, need to import third-party library: asihttprequest,cocoahttpserver,m3u8 (where ASI is used for network requests, Cocoahttpserver for server use on iOS, m3u8 is used to parse the returned index file)


ASI Configuration Considerations
MRC Error Handling

2, Import System library: Libsqlite3.dylib, Libz.dylib, Libxml2.dylib, Coretelephony.framework, Systemconfiguration.framework, Mobilecoreservices.framework, Security.framework, Cfnetwork.framework, mediaplayer.framework

3. Add header File

YCHLS-Demo.h

4. Demo Introduction


Demo Style
    • Play: directly play online live link, is completed by the system Mpmovieplayer, it comes with the analysis of the function of the HLS live chain.
    • Download: Follow the HLS protocol to download the relevant video slices from the index file's resource path and save them to your phone locally.
    • play local Video: use the downloaded video file clips for continuous playback.
    • Clear cache: Delete downloaded video clips.

Principle:

    1. Through the ASI request link, parse the returned m3u8 index file through the m3u8 library.
    2. Then through the ASI download the parsed video resource address, modeled in the HLS file storage path storage.
    3. Use Cocoahttpserver to build a local server on the iOS side and turn on the service with the port number: 12345 (High port). The configuration server path is consistent with step two storage paths.
    4. Set the player live link as the local server address, play directly, because the player adheres to the HLS protocol, it is able to resolve the local server address that we previously built using the HLS protocol.
    5. Click Play Online to verify that it matches the local playback effect.

HLS Protocol file storage structure

Above is the map of the server store video file slices and index files in HLS
The whole process is:

    1. Click Download First to parse the m3u8 third-party library to resolve the resource. (The m3u8 library can only parse a specific format of the m3u8 file, the code will be labeled)
    2. Click Play local video to play the downloaded resources.
    3. Click Play is used to preview the effect of the live stream, regardless of the entire process.
    4. The progress bar is used to display the download progress.

Summarize:
The demo doesn't just let us build a HLS server or a HLS-enabled player. The purpose is to understand the implementation of the HLS protocol and some of the physical architectures on the server side. Through the demo learning, you can learn more about the specific implementation of HLS live process.

Part of the source code posted

To turn on the local server:

- (void) openhttpserver{Self. httpserver = [[Httpserver alloc] init]; [Self. Httpserver SetType:@ "_http._tcp."];Set the service type [self.httpserver setport:12345] ; //set the server port //get the Library/cache path under the local downloads path nsstring *webpath = [Klibrarycache stringbyappendingpathcomponent:kpathdownload]; nslog (@ "-------------\\nSetting document Root:%@\\n", Webpath); //set server path [self.httpServer Setdocumentroot:webpath]; nserror *error; if (![ self.httpserver Start:&error]) {nslog (@ "-------------\\nError starting HTTP Server:%@\\n", Error); } 

Video Download:

- (ibaction) Downloadstreamingmedia: (ID) Sender {UIButton *downloadbutton = sender;Get local Library/cache PathNSString *localdownloadspath = [Klibrarycache stringbyappendingpathcomponent:kpathdownload];Get video Local PathNSString *filepath = [Localdownloadspath stringbyappendingpathcomponent:@ "xnjuxmte4ndaw/movie.m3u8"];Nsfilemanager *filemanager = [nsfilemanager Defaultmanager]; //Determine if the video cache is complete and play local cache if done ([FileManager Fileexistsatpath:filepath]) {[Downloadbutton settitle:@ ' completed ' Forstate:UIControlStateNormal]; Downloadbutton. Enabled = NO;} else{M3u8handler *handler = [[M3u8handler alloc] init]; handler. Delegate = self ; //Parse m3u8 video address [handler Praseurl:test_hls_url]; //Turn on the network indicator [[[uiapplication sharedapplication] setnetworkactivityindicatorvisible:YES];}}  

Play local Video:

- (ibaction) Playvideofromlocal: (ID) Sender {NSString * Playurl = [NSString stringWithFormat:@ "http://127.0.0.1:12345/XNjUxMTE4NDAw/movie.m3u8"];NSLog (@ "Local Video address-----%@", playurl);Get local Library/cache PathNSString *localdownloadspath = [Klibrarycache stringbyappendingpathcomponent:kpathdownload];Get video Local PathNSString *filepath = [Localdownloadspath stringbyappendingpathcomponent:@ "xnjuxmte4ndaw/movie.m3u8"];Nsfilemanager *filemanager = [Nsfilemanager Defaultmanager];//determine if the video cache is complete, and if done then play the local cache if ([FileManager Fileexistsatpath: FilePath]) {mpmovieplayerviewcontroller *playerviewcontroller =[[mpmovieplayerviewcontroller alloc]initwithcontenturl:[NSURL Urlwithstring:playurl]]; [self Presentmovieplayerviewcontrolleranimated:playerviewcontroller];} else{uialertview *alertview = [[ uialertview alloc] initwithtitle:@ "Sorry" Message:@ "current video not Cached" Delegate:self cancelbuttontitle: @ "OK" Otherbuttontitles:nil, nil ]; [Alertview show]; }}

Play online video

- (IBAction)playLiveStreaming {    NSURL *url = [[NSURL alloc] initWithString:TEST_HLS_URL];    MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; [self presentMoviePlayerViewControllerAnimated:player];}



Wen/da Ai Ali Hato (Jane book author)
Original link: http://www.jianshu.com/p/e5ca0d65dde6
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

How do I use iOS for live, on-demand, and cache of videos? -http Live Streaming (HLS)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.