Created
April 3, 2013 14:31
-
-
Save Lewuathe/5301729 to your computer and use it in GitHub Desktop.
How to play movies on UIView? ref: http://qiita.com/items/4a4d2668dce767ee828b
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <UIKit/UIKit.h> | |
#import <AVFoundation/AVFoundation.h> | |
@interface AVPlayerView : UIView | |
@end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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