xcode 媒体播放器框架 iOS SDK
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8569358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MediaPlayer Framework iOS SDK
提问by user964627
I am trying to implement video into my app and I am having a hard time. I have tried thistutorial and it was super helpful.
我正在尝试在我的应用程序中实现视频,但我遇到了困难。我已经尝试过本教程,它非常有帮助。
But now when I try to run my app and click on the button, the video frame comes up and just stays black. My video is in the correct format, I also made sure that I am pulling in the MediaPlayer Framework. Has anyone ran into this issue before or knows why this would be happening?
但是现在当我尝试运行我的应用程序并单击按钮时,视频帧出现并保持黑色。我的视频格式正确,我还确保我正在拉入 MediaPlayer 框架。有没有人以前遇到过这个问题或知道为什么会发生这种情况?
This is what I have:
这就是我所拥有的:
-(IBAction)playMovie:(id)sender
{
UIButton *playButton = (UIButton *) sender;
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"big-buck-bunny-clip" ofType:@"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filepath];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayerController];
[moviePlayerController.view setFrame:CGRectMake(38, 100, 250, 163)];
[self.view addSubview:moviePlayerController.view];
//moviePlayerController.fullscreen = YES;
moviePlayerController.initialPlaybackTime = 5;
[moviePlayerController play];
}
回答by Eugene
Have you added MPMoviePlayerController
's view onto your view?
您是否将MPMoviePlayerController
的视图添加到您的视图中?
MPMoviePlayerController *movieController = ...;
movieController.view.frame = self.view.bounds;
[self.view addSubview:movieController.view];
Frame could also be black if you've set shouldAutoplay
to YES and controlStyle to MPMovieControlStyleNone
如果您已设置shouldAutoplay
为 YES 并且 controlStyle 为MPMovieControlStyleNone
--Edit This is how I init my player and it works, maybe that will be of some help. It's playing HTTP Live Streaming video, but it'll play anything you put into it. You should try the url in this sample. If it'll work then there's definitely some problem with your contentUrl.
--Edit 这就是我如何初始化我的播放器并且它可以工作,也许这会有所帮助。它正在播放 HTTP Live Streaming 视频,但它会播放您放入其中的任何内容。您应该尝试此示例中的网址。如果它可以工作,那么你的 contentUrl 肯定有问题。
url = [NSURL URLWithString:@"http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8"];
self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:url];
moviePlayerController.view.frame = self.view.bounds;
[self.view insertSubview:moviePlayerController.view atIndex:0];
moviePlayerController.controlStyle = MPMovieControlStyleNone;
moviePlayerController.shouldAutoplay = YES;
[moviePlayerController prepareToPlay];
回答by Erpheus
I had the same problem, and also following the same tutorial, but then I found the class moviePlayerViewController that its easier to use and you don't even have to worry about dismiss buttons.
我遇到了同样的问题,并遵循了相同的教程,但后来我发现类 moviePlayerViewController 更易于使用,您甚至不必担心关闭按钮。
I fixed used the following code: (I think it works better with ios5)
我修复了使用以下代码:(我认为它在 ios5 上效果更好)
self.moviePlayerViewController = [[MPMoviePlayerViewController alloc] initWithContentURL:finalurl];
moviePlayerViewController.view.frame = self.view.bounds;
[self presentMoviePlayerViewControllerAnimated:moviePlayerViewController];
moviePlayerViewController.moviePlayer.controlStyle = MPMovieControlStyleFullscreen;
moviePlayerViewController.moviePlayer.shouldAutoplay = YES;
[moviePlayerViewController.moviePlayer prepareToPlay];
moviePlayerViewController.moviePlayer.fullscreen=YES;