BGPlayVideoController.m 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //
  2. // BGPlayVideoController.m
  3. // BuguLive
  4. //
  5. // Created by fanwe2014 on 2017/4/10.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "BGPlayVideoController.h"
  9. #import <MediaPlayer/MediaPlayer.h>
  10. #import <AVFoundation/AVFoundation.h>
  11. #import <AVKit/AVKit.h>
  12. @interface BGPlayVideoController ()
  13. {
  14. AVPlayer *_player; //视频播放器
  15. AVPlayerViewController *_playerController;
  16. AVAudioSession *_session;
  17. }
  18. @property (nonatomic,strong)MPMoviePlayerController *mp;
  19. @end
  20. @implementation BGPlayVideoController
  21. - (void)viewDidLoad
  22. {
  23. [super viewDidLoad];
  24. [self creatMainView];
  25. }
  26. - (void)creatMainView
  27. {
  28. self.view.backgroundColor = [UIColor blackColor];
  29. _session = [AVAudioSession sharedInstance];
  30. [_session setCategory:AVAudioSessionCategoryPlayback error:nil];
  31. _player = [AVPlayer playerWithURL:[NSURL URLWithString:self.playUrl]];
  32. _playerController = [[AVPlayerViewController alloc] init];
  33. _playerController.player = _player;
  34. _playerController.videoGravity = AVLayerVideoGravityResizeAspect;
  35. _playerController.allowsPictureInPicturePlayback = true; //画中画,iPad可用
  36. _playerController.showsPlaybackControls = true;
  37. [self addChildViewController:_playerController];
  38. _playerController.view.translatesAutoresizingMaskIntoConstraints = true; //AVPlayerViewController 内部可能是用约束写的,这句可以禁用自动约束,消除报错
  39. _playerController.view.frame = CGRectMake(0, 0, kScreenW, kScreenH);
  40. [self.view addSubview:_playerController.view];
  41. [_playerController.player play]; //自动播放
  42. UIButton *btn =[UIButton buttonWithType:UIButtonTypeCustom];
  43. if ([UIDevice currentDevice].systemVersion.doubleValue > 11.0)
  44. {
  45. btn.frame=CGRectMake(10, kStatusBarHeight, 50, 50);
  46. }else
  47. {
  48. btn.frame=CGRectMake(10, 70, 50, 50);
  49. }
  50. btn.layer.cornerRadius = 25;
  51. btn.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.2];
  52. [btn setImage:[UIImage imageNamed:@"fw_me_comeBack"] forState:UIControlStateNormal];
  53. [btn addTarget:self action:@selector(goBakc) forControlEvents:UIControlEventTouchUpInside];
  54. [self.view addSubview:btn];
  55. }
  56. //返回按钮
  57. - (void)goBakc
  58. {
  59. [self.navigationController popViewControllerAnimated:YES];
  60. }
  61. - (void)viewWillAppear:(BOOL)animated
  62. {
  63. [super viewWillAppear:animated];
  64. [self.navigationController setNavigationBarHidden:YES animated:animated];
  65. [[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
  66. }
  67. //恢复系统导航栏的默认样式
  68. - (void)viewWillDisappear:(BOOL)animated
  69. {
  70. [super viewWillDisappear:animated];
  71. [self.navigationController setNavigationBarHidden:NO animated:animated];
  72. [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
  73. _session = nil;
  74. _playerController = nil;
  75. _player = nil;
  76. }
  77. - (void)viewDidDisappear:(BOOL)animated
  78. {
  79. [super viewDidDisappear:animated];
  80. [[UIApplication sharedApplication]setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
  81. _session = nil;
  82. _playerController = nil;
  83. _player = nil;
  84. }
  85. @end