显示进度条,直到视频加载IOS7 [英] show progress bar until video loads IOS7

查看:92
本文介绍了显示进度条,直到视频加载IOS7的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我的应用程序我正在使用URL播放视频。我现在通过我的服务器传递视频网址,这是他花费大量时间播放视频的问题。所以我想显示进度条直到它加载视频。

Hi in my application I'm playing the video using URL. I'm passing the video URL form my server now the problem its taking to much time to play the video. So i want to show progress bar until its load the video .

所以我使用MBProgressHUD进度条显示进度条但视频没有播放请告诉我做错了。

So i have used the MBProgressHUD for progress bar its showing the progress bar but the video is not playing please tell where I'm doing wrong.

我的代码。

- (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];


}

我使用了上面的代码而不是播放视频请告诉我在上面的代码中我做错了如何实现这个。

I have used the above code its not playing the video please tell where I'm doing wrong in the above code how to achieve this one.

谢谢。

推荐答案

1.创建新视图控件

2.通过你的videoarray1值到nextvieww控件我的意思是你的视频网址

2.pass your videoarray1 value to nextvieww control i mean your video url

3.add代码nextview control

3.add following code nextview control

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    self.indicator.frame = CGRectMake(0.0, 0.0, 40.0, 40.0);
    self.indicator.center = self.view.center;
    [self.view addSubview:self.indicator];
    [self.indicator startAnimating];

    _movieplayer = [[MPMoviePlayerController alloc]initWithContentURL: [NSURL URLWithString:[self urlencode:self.strPlayUrl]]];
    [[_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];

    //this is the line we need to do
    [self.view insertSubview:self.movieplayer.view belowSubview:self.indicator];
    [self.movieplayer play];
}
- (void)viewDidAppear:(BOOL)animated {
    NSLog(@"VIEW DID LOAD");
    // Register to receive a notification that the movie is now in memory and ready to play
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(movieLoadStateDidChange:)
                                                 name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:nil];

}

-(void)movieLoadStateDidChange:(id)sender
{
    NSLog(@"STATE CHANGED");
    if(MPMovieLoadStatePlaythroughOK ) {
        NSLog(@"State is Playable OK");
        NSLog(@"Enough data has been buffered for playback to continue uninterrupted..");
         self.indicator.hidden = YES;
        [ self.indicator stopAnimating];
    }

}


-(NSString *)urlencode:(NSString *)str
{
    NSString *encodeString=(NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)str, NULL, (CFStringRef)@"", kCFStringEncodingUTF8));
    return encodeString;
}

这篇关于显示进度条,直到视频加载IOS7的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆