歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> iOS視頻播放代碼

iOS視頻播放代碼

日期:2017/3/1 10:12:29   编辑:Linux編程

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. #pragma mark -------------------視頻播放結束委托--------------------
  23. /*
  24. @method 當視頻播放完畢釋放對象 
  25. */
  26. -(void)myMovieFinishedCallback:(NSNotification*)notify
  27. {
  28. //視頻播放對象
  29. MPMoviePlayerController* theMovie = [notify object];
  30. //銷毀播放通知
  31. [[NSNotificationCenter defaultCenter] removeObserver:self
  32. name:MPMoviePlayerPlaybackDidFinishNotification
  33. object:theMovie];
  34. [theMovie.view removeFromSuperview];
  35. // 釋放視頻對象
  36. [theMovie release];
  37. }
Copyright © Linux教程網 All Rights Reserved