AVPlayerViewController video player, avplayercontroller
Preface
After iOS8, the system uses AVPlayerViewController to play videos.
AVPlayerViewController
AVPlayerViewController is similar to the navigation controller. You need to add it to a View Controller and add its view to the view.
1. Import the header file
#import <AVKit/AVKit.h>#import <AVFoundation/AVFoundation.h>
2. Declare member variables
AVPlayerViewController *_playerController; AVPlayer *_player; AVAudioSession *_session; NSString *_urlString;
3. Use a player
-(Void) viewDidLoad {[super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self. view. backgroundColor = [UIColor whiteColor]; _ session = [AVAudioSession sharedInstance]; [_ session setCategory: AVAudioSessionCategoryPlayback error: nil]; _ player = [AVPlayer playerWithURL: [NSURL URLWithString: @ "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4"]; _ playerController = [[AVPlayerViewController alloc] init]; _ playerController. player = _ player; _ playerController. videoGravity = AVLayerVideoGravityResizeAspect; _ playerController. allowsPictureInPicturePlayback = true; // picture-in-picture, iPad _ playerController. showsPlaybackControls = true; [self addChildViewController: _ playerController]; _ playerController. view. translatesAutoresizingMaskIntoConstraints = true; // AVPlayerViewController may be written with constraints internally. This statement can disable automatic constraints and eliminate the error/self. view. bounds _ playerController. view. frame = CGRectMake (0, 0,320,300); [self. view addSubview: _ playerController. view]; [_ playerController. player play]; // automatic playback}
4. If you want to use an iOS9 image in-picture (tablet only)
: