博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
用 MPMoviePlayerController 实现简单的视频下载播放功能
阅读量:6767 次
发布时间:2019-06-26

本文共 4670 字,大约阅读时间需要 15 分钟。

初始化MPMoviePlayerController

  MovieView = [[MPMoviePlayerController alloc]init];    [MovieView.view setFrame:CGRectMake(145, 0, 1024-145, 651)];    [MovieView setControlStyle:MPMovieControlModeVolumeOnly];    [MovieView setFullscreen:YES animated:YES];    MovieView.repeatMode = MPMovieRepeatModeOne;    [MovieView setInitialPlaybackTime:0.0];    MovieView.movieSourceType = MPMovieSourceTypeFile;    MovieView.scalingMode = MPMovieScalingModeAspectFit;    [self.view addSubview:MovieView.view];

判断本地是否有视频,没的的话下载然后播放,有的话直接播放本地视频

NSURL *playerFileURL;    if ([[[arryImageInfo objectAtIndex:0] objectForKey:@"outlink"]isEqualToString:@""]) {        playerFileURL =[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[DataPist shared].ipString,[[arryImageInfo objectAtIndex:0] objectForKey:@"video"]]];    }else    {        playerFileURL =[NSURL URLWithString:[[arryImageInfo objectAtIndex:0] objectForKey:@"outlink"]];    }        NSString *movieNames=[NSString stringWithFormat:@"%@.mp4",[[arryImageInfo objectAtIndex:0] objectForKey:@"name"]];    if ([[NSFileManager defaultManager]fileExistsAtPath:[DataPist getFilePath:movieNames]]) {        NSString *moviePath=[NSString stringWithFormat:@"%@",[DataPist getFilePath:movieNames]];        NSURL *url=[[NSURL alloc]initFileURLWithPath:moviePath];        [MovieView setContentURL:url];        [MovieView.view setHidden:NO];//        [MovieView thumbnailImageAtTime:1.0 timeOption:MPMovieTimeOptionNearestKeyFrame];        [MovieView play];    }else    {        for ( int i=0; i

实现下载功能

-(void)download:(NSURL *)url tag:(int )tag{        ASIHTTPRequest *request=[ASIHTTPRequest requestWithURL:url];        [request setDelegate:self];        movieName=[NSString stringWithFormat:@"%@.mp4",[[arryImageInfo objectAtIndex:tag] objectForKey:@"name"]];        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);        NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:movieName];        NSLog(@"%@",documentsDirectory);        [request setAllowResumeForFileDownloads:YES];        [request setDownloadDestinationPath:documentsDirectory];        [request setDidReceiveResponseHeadersSelector:@selector(didReceiveResponseHeadersSelector:)];        [request setDidReceiveDataSelector:@selector(request:didReceiveData:)];        [request setDidFailSelector:@selector(Failed:)];        [request setDidFinishSelector:@selector(Finished:)];        [request startAsynchronous];}-(void)didReceiveResponseHeadersSelector:(ASIHTTPRequest *)request{    NSString  *string=[[NSString alloc]initWithFormat:@"%@",[request.responseHeaders valueForKey:@"Content-Length"]];    byteall=[string integerValue];    NSLog(@"byteall=%i M",byteall/1024/1024);}-(void)request:(ASIHTTPRequest *)request didReceiveData:(NSData *)Data{    [DataAll appendData:Data];    [TanChuView setHidden:NO];    NSLog(@"%lli M",(long long)[DataAll length]/1024/1024);    LoadLabel.text=[NSString stringWithFormat:@"共%i M,正在下载%lli M",byteall/1024/1024,(long long)[DataAll length]/1024/1024]; }-(void)Finished:(ASIHTTPRequest *)request{    NSLog(@"下载完成");    LoadLabel.text=@"下载完成,开始播放";    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);    NSString *documentsDirectory = [[paths objectAtIndex:0] stringByAppendingPathComponent:movieName];    NSLog(@"%@",documentsDirectory);    [MBProgressHUD hideHUDForView:self.view animated:YES];    [DataAll writeToFile:documentsDirectory atomically:YES];//    sleep(1);    NSURL *url=[[NSURL alloc]initFileURLWithPath:documentsDirectory];    [MovieView setContentURL:url];    [MovieView.view setHidden:NO];    [MovieView play];    [TanChuView removeFromSuperview];        for ( int i=0; i

加载进度百分比的

-(void)waitView{    [TanChuView removeFromSuperview];    TanChuView=[[UIView alloc]initWithFrame:CGRectMake(300, 250, 200, 50)];    NSLog(@"-----%@",NSStringFromCGRect(TanChuView.frame));    TanChuView.backgroundColor=[UIColor whiteColor];    TanChuView.alpha=0.5;    TanChuView.layer.cornerRadius = 6;    TanChuView.layer.masksToBounds = YES;    LoadLabel=[[UILabel alloc]initWithFrame:CGRectMake(30, 10, 150, 30)];//    LoadLabel.text=@"正在下载";    LoadLabel.font=[UIFont systemFontOfSize:12];    LoadLabel.backgroundColor=[UIColor whiteColor];    [TanChuView addSubview:LoadLabel];    [MovieView.view addSubview:TanChuView];}

实现完成,主要用到了ASI下载,并显示下载进度,还有GCD 的异步下载,

另一种加载进度百分比显示:

- (void)setProgress:(float)newProgress

 

{

 

    progressView.progress = newProgress;

 

    [progressValuesetText:[NSStringstringWithFormat:@"%0.f%%",progressView.progress * 100]];

 

}

 

转载于:https://www.cnblogs.com/zhangsongbai/p/3441592.html

你可能感兴趣的文章
一个"如何使用示波器安全测试接市电电路板"的问题
查看>>
SQL Server 索引和视图
查看>>
checkbox前后台使用
查看>>
什么是java的事物
查看>>
Windows的SEH机理简要介绍
查看>>
[转载]深入理解SQL注入绕过WAF和过滤机制
查看>>
Django进阶
查看>>
Vue 路由的懒加载和组件的按需加载方法
查看>>
Koa框架的一些随记
查看>>
0423-mysql插入语句大全
查看>>
mui SQL分页加搜索功能 案例
查看>>
Sington单例模式(创建型模式)
查看>>
idea导入或者检出项目时发现编辑器左侧无法显示项目目录结构
查看>>
添加相关功能
查看>>
php基础
查看>>
html5+canvs实现flash效果。
查看>>
《JAVA与模式》之简单工厂与工厂方法
查看>>
Entity Framework公共的增删改方法
查看>>
虚拟机搭建hadoop环境
查看>>
面向对象15.3String类-常见功能-获取-2
查看>>