我想在我的iphone应用中播放一段简短的视频。当我使用下面的代码时,我只听到音频并看到 应用程序的常规视图。我想让视频在这个视图之上播放。 我该怎么办?
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"LEADER" ofType:@"mov"];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMovie.scalingMode = MPMovieScalingModeAspectFill;
[theMovie play];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
答案 0 :(得分:4)
请勿混淆MPMoviePlayerController
和MPMoviePlayerViewController
。当您使用MPMoviePlayerController
时,请使用它(通常用于iPad上的嵌入式视频):
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];
当您使用MPMoviePlayerViewController
时,会使用presentMoviePlayerViewControllerAnimated:
(通常用于全屏视频)展示视频。
答案 1 :(得分:0)
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player.view.frame = self.view.frame];
[self.view addSubview: player.view];
// ...
[player play];
答案 2 :(得分:0)
对我有用的唯一魔力是
- (void) playMovie {
NSURL *url = [NSURL URLWithString:
@"http://www.example.com/video.mp4"];
MPMoviePlayerController *controller = [[MPMoviePlayerController alloc]
initWithContentURL:url];
self.mc = controller; //Super important
controller.view.frame = self.view.bounds; //Set the size
[self.view addSubview:controller.view]; //Show the view
[controller play]; //Start playing
}
在页眉文件中
@property (nonatomic,strong) MPMoviePlayerController* mc;