最近在做网络视频的播放,在经过很多错误之后,终于做到了流媒体的播放,这里来资源共享一下
1. 视频边播边下的实质是通过使用ASIHTTPRequest的setTemporaryFileDownloadPath来设置临时下载路径,和
setDownloadDestinationPath来设置下载路径。ASIHTTPRequest包装好了流媒体的播放,使用起来非常简单,
核心的代码如下:
//下载完存储目录
[request setDownloadDestinationPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedi2.mp4"]]];
//临时存储目录
[request setTemporaryFileDownloadPath:[webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedi2.mp4"]]];
[request setBytesReceivedBlock:^(unsigned long long size, unsigned long long total) {
//用file_length把total放到userDefault里面去;
//这时userDefault里面的file_length就是视频的总大小
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setDouble:total forKey:@"file_length"];
Recordull += size;//Recordull全局变量,记录已下载的文件的大小
//设置最大缓冲,最多400M
if (!isPlay&&Recordull > 400000) {
isPlay = !isPlay; //如果缓冲过了400M,直接开始播放
[self playVideo];
}
}];
//断点续载
[request setAllowResumeForFileDownloads:YES];
[request startAsynchronous]; //开始请求数据,即开始下载
那么在播放视频这一块,可以直接使用官方给的MPMoviePlayerController,具体如何使用在这里就不一一细说了,大家谷歌一下出来一大堆。
但是记得在一开始的时候给加上消息推送,用来通知视频播放完毕后的动作,一般是把播放器设nil等操作。
2. 边播边下地具体实现
首先要设定下载的路径:
NSString *tempPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Temp"];
NSString *cachePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Private Documents/Cache"];
在设定完路径之后,记得先去看看路径是否存在,不然会报错。
NSFileManager *fileManager=[NSFileManager defaultManager];
//视频存放的目录是否存在,不存在就创建出来。
if(![fileManager fileExistsAtPath:cachePath])
{
<span style="white-space:pre"> </span>[fileManager createDirectoryAtPath:cachePath withIntermediateDirectories:YES attributes:nil error:nil];
}
之后就应该开始播放视频了,先看看视频下载过了没有,肯定不能每一次看都要重新下载。如果视频存在于cachePath里面,说明该视频已经下载过了,可以直接本地看
if ([fileManager fileExistsAtPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedioName.mp4"]]])
{
MPMoviePlayerViewController *playerViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedioName.mp4"]]]];
//调用这个方法去播放视频
[self presentMoviePlayerViewControllerAnimated:playerViewController];
//此时不需要再下载了,所以就直接设nil;
videoRequest = nil;
}
如果这个视频是新的,没有下载过的,在设定完视频网络下载URL之后,就可以通过上面的核心代码块来进行下载了。
//设定视频的下载地址
ASIHTTPRequest *request=[[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:@"http://static.tripbe.com/videofiles/20121214/9533522808.f4v.mp4"]];
//下载完存储目录
[request setDownloadDestinationPath:[cachePath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedi2.mp4"]]];
//临时存储目录
[request setTemporaryFileDownloadPath:[webPath stringByAppendingPathComponent:[NSString stringWithFormat:@"vedi2.mp4"]]];
[request setBytesReceivedBlock:^(unsigned long long size, unsigned long long total) {
<span style="white-space:pre"> </span>//用file_length把total放到userDefault里面去;
//这时userDefault里面的就是视频的总大小
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setDouble:total forKey:@"file_length"];
Recordull += size;//Recordull全局变量,记录已下载的文件的大小
//设置最大缓冲,最多400M
if (!isPlay&&Recordull > 400000) {
isPlay = !isPlay; //如果缓冲过了400M,直接开始播放
[self playVideo]; //在这个方法里用MPMoviePlayerViewController开始播放视频
}
}];
//断点续载
[request setAllowResumeForFileDownloads:YES];
[request startAsynchronous]; //开始请求数据,即开始下载
videoRequest = request
例子的下载地址已经找不到了...
,我把例子上传到gitHub上给需要的大家下载吧:https://github.com/luochuhuaZai/VideoTest