iOS Streaming media

Source: Internet
Author: User
Tags file copy

http://my.oschina.net/CgShare/blog/302303

Progressive Download (pseudo-streaming media)

Between the download local playback and live streaming media a form of play, download local playback must all download the file before playing, and progressive download does not have to wait until all the download is complete before playing, it can be downloaded while playing, after the completion of the content, the entire file will be saved on the phone.

Live Streaming media

Real-time streaming is the one side of the receiving packet playback, the local does not keep the file copy, live streaming is always real-time transmission, real-time live broadcast, support random access, users can fast forward or rewind to view the front or back of the content. Real-time streaming media transmission must ensure that the transmission speed of the packet is greater than the speed of the file, or the user will see a video pause. Video quality degrades when the network is plugged, so it's better to ensure that the quality of the video is progressively downloaded.

Real-time Streaming media protocol:

RTSP (Real time streaming Protocol)

MMS (Microsoft Media Server Protocol)

HLS (Http Live streaming)

Here is the main introduction of HLS,

HLS (HTTP Live streaming) is an HTTP-based streaming solution developed by Apple for mobile devices such as the iphone, IPod, itouch and ipad

https://developer.apple.com/streaming/

Technical key points

1. Capture data from video sources and audio sources

2. H264 encoding and AAC encoding of raw data

3. Video and audio data encapsulated as Mpeg-ts package

4.HLS segmentation Generation Strategy and m3u8 index file

5.HTTP Transport Protocol

Setting up HLS Streaming media server

Apache HTTP Server (Apple comes with)

Tomcat Web Server

IIS (Internet information Services)

Only Apache HTTP Server is recommended here

Open Terminal, vi/etc/apache2/httpd.conf

Under the <ifmodule mime_module>

Add two lines

AddType application/x-mpegurl.m3u8

AddType video/mp2t.ts

Perhaps you have insufficient authority, then use sudo chmod 777/etc/apache2/httpd.conf

Then vi/etc/apache2/httpd.conf

Restarting the server

sudo apachectl restart

==============================================

or build XMPP server or not set up, favorably get m3u8

==============================================

Create a project

Download library from git: Http://git.oschina.net/1213125967/HLS

Import a library into a project

Need to introduce a third party open source framework: asihttprequest,cocoahttpserver,m3u8

The system framework needs to be imported: Libsqlite3.dylib, Libz.dylib, Libxml2.dylib, Coretelephony.framework, Systemconfiguration.framework, Mobilecoreservices.framework, Security.framework, Cfnetwork.framework, mediaplayer.framework

Add/USR/INCLUDE/LIBXML2 to the library search path

Add header File

?
1234 #import <MediaPlayer/MediaPlayer.h>#import "M3U8Handler.h"#import "VideoDownloader.h"#import "HTTPServer.h"

Declaring attributes:

?
12 @property (nonatomic, strong)HTTPServer * httpServer;@property (nonatomic, strong)VideoDownloader *downloader;

Pre-play, Piscian set up the server

?
1234567891011121314151617181920212223 #pragma mark - 打开本地服务器- (void)openHttpServer{    self.httpServer = [[HTTPServer alloc] init];    [self.httpServer setType:@"_http._tcp."];  // 设置服务类型    [self.httpServer setPort:12345]; // 设置服务器端口        // 获取本地Documents路径    NSString *pathPrefix = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];        // 获取本地Documents路径下downloads路径    NSString *webPath = [pathPrefix stringByAppendingPathComponent:kPathDownload];    NSLog(@"-------------\nSetting document root: %@\n", webPath);         // 设置服务器路径    [self.httpServer setDocumentRoot:webPath];    NSError *error;        if(![self.httpServer start:&error])    {        NSLog(@"-------------\nError starting HTTP Server: %@\n", error);    }}

When the building is finished, what is played depends on the needs

Stream Media Playback Online

?
123456     // 优酷视频m3u8新地址格式如下:http://pl.youku.com/playlist/m3u8?vid=XNzIwMDE5NzI4&type=mp4    // 如果上面的链接不可用,那么使用这个链接http://v.youku.com/player/getM3U8/vid/XNzIwMDE5NzI4/type/flv    NSURL *url = [[NSURL alloc] initWithString:@"http://v.youku.com/player/getM3U8/vid/XNzIwMDE5NzI4/type/mp4"];    MPMoviePlayerViewController *player = [[MPMoviePlayerViewController alloc] initWithContentURL:url];        [self presentMoviePlayerViewControllerAnimated:player];

Video download

?
1234567     M3U8Handler *handler = [[M3U8Handler alloc] init];    handler.delegate = self;    // 解析m3u8视频地址    [handler praseUrl:[NSString stringWithFormat:@"http://pl.youku.com/playlist/m3u8?vid=XNzIwMDE5NzI4&type=mp4"]];        // 开启网络指示器    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

Play local video

?
12345678910111213141516171819202122     NSString * playurl = [NSString stringWithFormat:@"http://127.0.0.1:12345/XNzIwMDE5NzI4/movie.m3u8"];    NSLog(@"本地视频地址-----%@", playurl);         // 获取本地Documents路径    NSString *pathPrefix = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES) objectAtIndex:0];        // 获取本地Documents路径下downloads路径    NSString *localDownloadsPath = [pathPrefix stringByAppendingPathComponent:kPathDownload];         // 获取视频本地路径    NSString *filePath = [localDownloadsPath stringByAppendingPathComponent:@"XNzIwMDE5NzI4/movie.m3u8"];    NSFileManager *fileManager = [NSFileManager defaultManager];        // 判断视频是否缓存完成,如果完成则播放本地缓存    if([fileManager fileExistsAtPath:filePath]) {        MPMoviePlayerViewController *playerViewController =[[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL URLWithString: playurl]];        [self presentMoviePlayerViewControllerAnimated:playerViewController];    }    else{        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Sorry"message:@"当前视频未缓存"delegate:self cancelButtonTitle:@"确定"otherButtonTitles:nil, nil];        [alertView show];    }

Add Agent <M3U8HandlerDelegate,VideoDownloadDelegate>

?
12345678910111213141516171819202122232425262728293031323334 #pragma mark --------------视频解析完成-----------------(void)praseM3U8Finished:(M3U8Handler*)handler{    handler.playlist.uuid= @"XNzIwMDE5NzI4";    self.downloader = [[VideoDownloader alloc]initWithM3U8List:handler.playlist];    [self.downloader addObserver:self forKeyPath:@"totalprogress"options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];    self.downloader.delegate = self;    [self.downloader startDownloadVideo];}-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void*)context{    NSLog(@"下载进度 - %f", self.downloader.totalprogress);}#pragma mark --------------视频解析失败-----------------(void)praseM3U8Failed:(M3U8Handler*)handler{    NSLog(@"视频解析失败-failed -- %@",handler);}#pragma mark --------------视频下载完成-----------------(void)videoDownloaderFinished:(VideoDownloader*)request{    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];    [request createLocalM3U8file];    NSLog(@"----------视频下载完成-------------");}#pragma mark --------------视频下载失败-----------------(void)videoDownloaderFailed:(VideoDownloader*)request{    NSLog(@"----------视频下载失败-----------");

iOS Streaming media

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.