Skip to content
This repository has been archived by the owner on Aug 26, 2021. It is now read-only.

vimeo/VIMVideoPlayer

Repository files navigation

VIMVideoPlayer

VIMVideoPlayer is a simple wrapper around the AVPlayer and AVPlayerLayer classes.

Setup

Add the VIMVideoPlayerView and VIMVideoPlayer classes to your project.

Do this by including this repo as a git submodule or by using cocoapods:

# Add this to your podfile
target 'MyTarget' do
	pod 'VIMVideoPlayer', {version_number}
end

Usage

Create a new VIMVideoPlayerView and add it to your view hierarchy:

#import "VIMVideoPlayerView.h"

...

- (void)viewDidLoad
{
    [super viewDidLoad];
  
    self.videoPlayerView = [[VIMVideoPlayerView alloc] init];
    self.videoPlayerView.translatesAutoresizingMaskIntoConstraints = NO;
    self.videoPlayerView.delegate = self;

    [self.videoPlayerView setVideoFillMode:AVLayerVideoGravityResizeAspect];
    [self.videoPlayerView.player enableTimeUpdates];
    [self.videoPlayerView.player enableAirplay];

    [self.view addSubview:self.videoPlayerView];

    NSDictionary *views = NSDictionaryOfVariableBindings(_videoPlayerView);
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|-0-[_videoPlayerView]-0-|" options:0   metrics:nil views:views]];
    [self.view addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|-0-[_videoPlayerView]-0-|" options:0   metrics:nil views:views]];
}

Play a video:

// Using an NSURL

NSURL *URL = ...; 
[self.videoPlayerView.player setURL:URL];
[self.videoPlayerView.player play];

/* 
  Note: This must be a URL to an actual video resource (e.g. http://website.com/video.mp4 or .m3u8 etc.),
  It cannot be a URL to a web page (e.g. https://vimeo.com/67069182),
  See below for info on using VIMVideoPlayer to play Vimeo videos.
*/

// Using an AVPlayerItem

AVPlayerItem *playerItem = ...;
[self.videoPlayerView.player setPlayerItem:playerItem];
[self.videoPlayerView.player play];

// Or using an AVAsset

AVAsset *asset = ...;
[self.videoPlayerView.player setAsset:asset];
[self.videoPlayerView.player play];

Optionally implement the VIMVideoPlayerViewDelegate protocol methods:

@protocol VIMVideoPlayerViewDelegate <NSObject>

@optional
- (void)videoPlayerViewIsReadyToPlayVideo:(VIMVideoPlayerView *)videoPlayerView;
- (void)videoPlayerViewDidReachEnd:(VIMVideoPlayerView *)videoPlayerView;
- (void)videoPlayerView:(VIMVideoPlayerView *)videoPlayerView timeDidChange:(CMTime)cmTime;
- (void)videoPlayerView:(VIMVideoPlayerView *)videoPlayerView loadedTimeRangeDidChange:(float)duration;
- (void)videoPlayerView:(VIMVideoPlayerView *)videoPlayerView didFailWithError:(NSError *)error;

@end

See VIMVideoPlayer.h for additional configuration options.

Playing Vimeo Videos

Vimeo Pro members can access playback URLs for Vimeo videos using the Vimeo API. Playback URLs are only included in the response object if the requesting account is a Vimeo Pro account.

If you have a Vimeo Pro account, when you make a request to the Vimeo API for a video object the response object will contain a list of video files. These represent the various resolution video files available for this particular video. Each has a link. You can use the string value keyed to link to create an NSURL. You can pass this NSURL to VIMVideoPlayer for playback.

Check out this Stack Overflow question for additional info.

You can use the Vimeo iOS SDK to interact with the Vimeo API.

For full documentation on the Vimeo API go here.

License

VIMVideoPlayer is available under the MIT license. See the LICENSE file for more info.

Questions?

Tweet at us here: @vimeoapi

Post on Stackoverflow with the tag vimeo-ios

Get in touch here