【发布时间】:2015-07-27 04:18:57
【问题描述】:
有没有办法像使用图像和图像视图一样将视频添加到视图中?我不想要 YouTube 应用中的全屏模式。
【问题讨论】:
有没有办法像使用图像和图像视图一样将视频添加到视图中?我不想要 YouTube 应用中的全屏模式。
【问题讨论】:
您可以使用 AVFoundation 框架中的 AVPlayer AVPlayer Demo
【讨论】:
试试这个代码。
1.导入媒体播放器框架-#import
2.声明实例变量 MPMoviePlayerController *player;
-(void)viewDidLoad
{
//you can try other api to get movie file path in ios 8
NSString *url = [[NSBundle mainBundle]
pathForResource:@"Trailer"
ofType:@"m4v"];
player = [[MPMoviePlayerController alloc]
initWithContentURL:[NSURL fileURLWithPath:url]];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
//—set the size of the movie view and then add it to the View window—
player.view.frame = CGRectMake(10, 10, 300, 300);
[self.view addSubview:player.view];
//—play movie—
[player play];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//—called when the movie is done playing—
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *moviePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
[moviePlayer.view removeFromSuperview];
}
//-(NSString *)path
//{
// NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, //NSUserDomainMask, YES);
// NSString *documentDir=[paths objectAtIndex:0];
// return [documentDir stringByAppendingPathComponent:@"Trailer.m4v"];
//}
【讨论】: