【发布时间】:2014-06-04 05:53:53
【问题描述】:
嗨,在我的应用程序中,我正在使用 URL 播放视频。我正在从我的服务器传递视频 URL,现在播放视频需要花费大量时间。所以我想在加载视频之前显示进度条。
所以我使用 MBProgressHUD 作为进度条,它显示进度条但视频没有播放,请告诉我哪里做错了。
我的代码。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString * currentVideo = [videoarray1 objectAtIndex:indexPath.row];
NSString *strurl=[self urlencode:currentVideo];
NSURL *url=[NSURL URLWithString:strurl];
NSURLRequest *req=[NSURLRequest requestWithURL:url];
NSURLConnection *con=[NSURLConnection connectionWithRequest:req delegate:self];
if (con) {
datas=[[NSMutableData alloc]init];
}
spinner = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
spinner.mode = MBProgressHUDModeCustomView;
[spinner setLabelText:@"Loading file....."];
[spinner setLabelFont:[UIFont systemFontOfSize:15]];
[spinner show:YES];
}
-(NSString *)urlencode:(NSString *)str
{
NSString *encodeString=(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)str, NULL, (CFStringRef)@"", kCFStringEncodingUTF8));
return encodeString;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSLog(@"%lld",[response expectedContentLength]);
self.length = [response expectedContentLength];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data1{
[datas appendData:data1];
float progress = (float)[datas length]/(float)self.length;
NSLog(@"%f",progress);
float check=progress*100;
if (check==100) {
[spinner hide:YES];
[spinner removeFromSuperViewOnHide];
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
NSData *data = datas;
NSLog(@"%@",data);
NSString* myurl;
myurl = [[NSString alloc] initWithData:datas encoding:NSASCIIStringEncoding];
NSLog(@"%@",myurl);
_movieplayer = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:myurl]];
[[_movieplayer view] setFrame: CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
[self.view addSubview: [_movieplayer view]];
[_movieplayer setShouldAutoplay:YES];
[_movieplayer prepareToPlay];
[_movieplayer play];
}
我使用了上面的代码它没有播放视频请告诉我在上面的代码中哪里做错了如何实现这一点。
谢谢。
【问题讨论】:
标签: ios video progress-bar