摘要:進式下載(偽流媒體) 介于下載本地播放與實時流媒體之間的一種播放形式弥雹,下載本地播放必須全部將文件下載完成后才能播放,而漸進式下載不必等到全部下載完成后再播放消别,它可以一邊下載一邊播放契沫,在完成播放內(nèi)容之后,整個文件會保存在手機上观堂。 實時流媒體 實時流媒體是一邊接收數(shù)據(jù)包一邊播放让网,本地不保...
進式下載(偽流媒體)
介于下載本地播放與實時流媒體之間的一種播放形式呀忧,下載本地播放必須全部將文件下載完成后才能播放,而漸進式下載不必等到全部下載完成后再播放溃睹,它可以一邊下載一邊播放而账,在完成播放內(nèi)容之后,整個文件會保存在手機上因篇。
實時流媒體
實時流媒體是一邊接收數(shù)據(jù)包一邊播放泞辐,本地不保留文件副本笔横,實時流式傳輸總是實時傳送,可以實時實況轉(zhuǎn)播咐吼,支持隨機訪問吹缔,用戶可以快進或者快退以觀看前面或后面的內(nèi)容。實時流媒體傳輸必須保證數(shù)據(jù)包的傳輸速度大于文件的播放速度锯茄,否則用戶看到的視頻會出現(xiàn)暫停厢塘。當網(wǎng)絡(luò)堵塞情況下視頻質(zhì)量會下降,所以要想保證視頻的質(zhì)量漸進式下載會更好一些肌幽。
實時流媒體協(xié)議:
RTSP(Real Time Streaming Protocol)
MMS(Microsoft Media Server protocol)
HLS(Http Live Streaming)
這里主要介紹HLS晚碾,
HLS(HTTP Live Streaming)是蘋果公司針對iPhone、iPod喂急、iTouch和iPad等移動設(shè)備而開發(fā)的基于HTTP協(xié)議的流媒體解決方案
https://developer.apple.com/streaming/
技術(shù)關(guān)鍵點
1.采集視頻源和音頻源的數(shù)據(jù)
2.對原始數(shù)據(jù)進行H264編碼和AAC編碼
3.視頻和音頻數(shù)據(jù)封裝為MPEG-TS包
4.HLS分段生成策略及m3u8索引文件
5.HTTP傳輸協(xié)議
搭建HLS流媒體服務器
Apache HTTP Server (蘋果自帶)
Tomcat Web Server
IIS(Internet Information Services)
這里只推薦Apache HTTP Server
打開終端格嘁,vi /etc/apache2/httpd.conf
在下
添加兩行
AddType application/x-mpegURL.M3U8
AddType video/MP2T.ts
可能你的權(quán)限不夠,那就用 sudo chmod 777 /etc/apache2/httpd.conf
然后 vi /etc/apache2/httpd.conf
重啟服務器
sudo apachectl restart
==============================================
或者搭建xmpp服務器 ?或者不搭建廊移,從優(yōu)酷獲取m3u8
==============================================
創(chuàng)建一個工程
從git中下載庫:http://git.oschina.net/1213125967/HLS
將庫導入工程
需要引入第三方開源框架:ASIHttpRequest糕簿,CocoaHTTPServer,m3u8
需要導入系統(tǒng)框架:libsqlite3.dylib狡孔、libz.dylib冶伞、libxml2.dylib、CoreTelephony.framework步氏、SystemConfiguration.framework响禽、MobileCoreServices.framework、Security.framework荚醒、CFNetwork.framework芋类、MediaPlayer.framework
在library search path 中添加 /usr/include/libxml2
添加頭文件
#import
#import?"M3U8Handler.h"
#import?"VideoDownloader.h"
#import?"HTTPServer.h"
聲明屬性:
@property (nonatomic, strong)HTTPServer * httpServer;
@property?(nonatomic,?strong)VideoDownloader?*downloader;
預先播放,畢先設(shè)置服務器
#pragma mark - 打開本地服務器
-?(void)openHttpServer
{
self.httpServer?=?[[HTTPServer?alloc]?init];
[self.httpServer?setType:@"_http._tcp."];//?設(shè)置服務類型
[self.httpServer?setPort:12345];//?設(shè)置服務器端口
//?獲取本地Documents路徑
NSString?*pathPrefix?=?[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES)?objectAtIndex:0];
//?獲取本地Documents路徑下downloads路徑
NSString?*webPath?=?[pathPrefix?stringByAppendingPathComponent:kPathDownload];
NSLog(@"-------------\nSetting?document?root:?%@\n",?webPath);
//?設(shè)置服務器路徑
[self.httpServer?setDocumentRoot:webPath];
NSError?*error;
if(![self.httpServer?start:&error])
{
NSLog(@"-------------\nError?starting?HTTP?Server:?%@\n",?error);
}
}
搭建完成后界阁,播放什么的侯繁,都是取決于需求
在線流媒體播放
// 優(yōu)酷視頻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];
視頻下載
M3U8Handler *handler = [[M3U8Handler alloc] init];
handler.delegate?=?self;
//?解析m3u8視頻地址
[handler?praseUrl:[NSString?stringWithFormat:@"http://pl.youku.com/playlist/m3u8?vid=XNzIwMDE5NzI4&type=mp4"]];
//?開啟網(wǎng)絡(luò)指示器
[[UIApplication?sharedApplication]?setNetworkActivityIndicatorVisible:YES];
播放本地視頻
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];
}
添加代理 <M3U8HandlerDelegate贮竟, VideoDownloadDelegate >
#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(@"----------視頻下載失敗-----------");
}