Skip to content

Instantly share code, notes, and snippets.

@Lewuathe
Created April 3, 2013 14:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Lewuathe/5301729 to your computer and use it in GitHub Desktop.
Save Lewuathe/5301729 to your computer and use it in GitHub Desktop.
How to play movies on UIView? ref: http://qiita.com/items/4a4d2668dce767ee828b
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
@interface AVPlayerView : UIView
@end
#import "AVPlayerView.h"
@implementation AVPlayerView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
// AVPlayerLayerが使えますよと教えてあげる
// これなしで、普通のUIViewにくっつけても動かないので大事
+ (Class)layerClass
{
return [AVPlayerLayer class];
}
@end
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import <CoreMedia/CoreMedia.h>
#import <AVFoundation/AVFoundation.h>
#import "AVPlayerView.h"
@interface MoviesViewController : UIViewController{
// あとでくっつける先となるUIViewの継承クラス
// 上で作ったやつ
IBOutlet AVPlayerView *playerView;
}
@end
#import "MoviesViewController.h"
@interface MoviesViewController () {
AVPlayer *videoPlayer;
NSString *moviePath;
}
@end
@implementation MoviesViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 今回はすでにDocuments以下に置いてある動画を再生する
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *directory = [paths objectAtIndex:0];
moviePath = [directory stringByAppendingPathComponent:@"/movie.mov"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
videoPlayer = [[AVPlayer alloc] initWithURL:movieURL];
AVPlayerLayer* layer = ( AVPlayerLayer* )playerView.layer;
layer.videoGravity = AVLayerVideoGravityResizeAspect;
layer.player = videoPlayer;
[videoPlayer1 play];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment