iOS视频播放代码


iOS视频播放代码

  1. /** 
  2.  @method 播放电影 
  3.  */  
  4. -(void)playMovie:(NSString *)fileName{  
  5.     //视频文件路径   
  6.     NSString *path = [[NSBundle mainBundle] pathForResource:fileName ofType:@"mp4"];  
  7.     //视频URL   
  8.     NSURL *url = [NSURL fileURLWithPath:path];  
  9.     //视频播放对象   
  10.     MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url];  
  11.     movie.controlStyle = MPMovieControlStyleFullscreen;  
  12.     [movie.view setFrame:self.view.bounds];  
  13.     movie.initialPlaybackTime = -1;  
  14.     [self.view addSubview:movie.view];  
  15.     // 注册一个播放结束的通知   
  16.     [[NSNotificationCenter defaultCenter] addObserver:self  
  17.                                              selector:@selector(myMovieFinishedCallback:)  
  18.                                                  name:MPMoviePlayerPlaybackDidFinishNotification  
  19.                                                object:movie];  
  20.     [movie play];  
  21. }  
  22.   
  23. #pragma mark -------------------视频播放结束委托--------------------   
  24.   
  25. /* 
  26.  @method 当视频播放完毕释放对象  
  27.  */  
  28. -(void)myMovieFinishedCallback:(NSNotification*)notify  
  29. {  
  30.     //视频播放对象   
  31.     MPMoviePlayerController* theMovie = [notify object];  
  32.     //销毁播放通知   
  33.     [[NSNotificationCenter defaultCenter] removeObserver:self  
  34.                                                     name:MPMoviePlayerPlaybackDidFinishNotification  
  35.                                                   object:theMovie];  
  36.     [theMovie.view removeFromSuperview];  
  37.     // 释放视频对象   
  38.     [theMovie release];  
  39. }  

相关内容