Objc - how to add a video player inside a UIView like this?

yellow picture yellow · Dec 11, 2012 · Viewed 17.2k times · Source

This player only have 2 buttons,play/pause and fullscreen,it looks like a system player,so fast and simple.

I used UIWebView and HTML5 inside the UIView,but UIWebView is so slow and can not play without fullscreen.

If this player is a UIObject or some UIView things,how can I add it?

thanks~

sample video player in app Everyday.me

Answer

Alessandro Pirovano picture Alessandro Pirovano · Dec 15, 2015

The MPMoviePlayerController is deprecated from iOS 9. You can insert a video into iOS project with AVPlayerViewController now.

For example:

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>

...

@property (nonatomic, retain) AVPlayerViewController *playerViewController;

...

- (void)viewDidLoad
{

    [super viewDidLoad];

    NSString *stringVideoName = @"video.m4v";
    NSString *stringVideoPath = [[NSBundle mainBundle] pathForResource:stringVideoName ofType:nil];
    NSAssert(stringVideoPath, @"Expected not nil video file");

    NSURL *urlVideoFile = [NSURL fileURLWithPath:stringVideoPath];
    NSAssert(urlVideoFile, @"Expected not nil video url");

    _playerViewController = [[AVPlayerViewController alloc] init];
    _playerViewController.player = [AVPlayer playerWithURL:urlVideoFile];
    _playerViewController.view.frame = self.view.bounds;
    _playerViewController.showsPlaybackControls = YES;

    [self.view addSubview:_playerViewController.view];
    self.view.autoresizesSubviews = YES;

}