初始化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<arryImageInfo.count; i++) { UIButton *btn=(UIButton *)[self.view viewWithTag:i]; btn.userInteractionEnabled=NO; } [self waitView]; dispatch_queue_t concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(concurrentQueue, ^{ dispatch_sync(concurrentQueue, ^{ /* Download the image here */ [self download:playerFileURL tag:0]; }); dispatch_sync(dispatch_get_main_queue(), ^{ /* Show the image to the user here on the main queue*/ }); }); } }
实现下载功能
-(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<arryImageInfo.count; i++) {
UIButton *btn=(UIButton *)[self.view viewWithTag:i]; btn.userInteractionEnabled=YES;
} } -(void)Failed:(ASIHTTPRequest *)Failed{ [MBProgressHUD hideHUDForView:self.view animated:YES]; UIAlertView *alter=[[UIAlertView alloc]initWithTitle:@"提示" message:@"播放失败,请查看网络是否连接" delegate:Nil cancelButtonTitle:@"确定" otherButtonTitles:Nil, nil]; [alter show]; [TanChuView removeFromSuperview]; for ( int i=0; i<arryImageInfo.count; i++) {
UIButton *btn=(UIButton *)[self.view viewWithTag:i]; btn.userInteractionEnabled=YES;
} }
加载进度百分比的
-(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]];
}