我是编写Objective-C的新手。我从Xcode 4.2开始。我发现很难找到学习的例子。
最近,我开始编写需要播放mp4视频的应用程序。 然后我发现MPMovieplayercontroller可以提供帮助。
这些是代码(从不同的例子得出结论):
-(void)play // a function that trigger by pressing a button
{
[self.view addSubview:self.player.view];
[self.player play];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor greenColor];
screen.backgroundColor = [UIColor redColor];
NSString *videoFilePath = [[NSBundle mainBundle] pathForResource:@"ted" ofType:@"mp4"];
if (videoFilePath == NULL)
{
return;
}
NSURL *videoURL =[NSURL fileURLWithPath:videoFilePath];
self.player.view.frame = CGRectMake(300,300, 400,400);
self.player = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
}
它不起作用。没有任何东西显示。我确信我的按钮会给出响应并调用正确的功能(播放)。
我还在运行时使用配置文件检查了应用程序。它说泄漏被发现了。 而现在我不知道我能做些什么。
我也是stackoverflow的新手。如果我以不正当的方式询问,请告诉我。谢谢。
答案 0 :(得分:1)
我遇到了同样的问题,因为MPMoviPlayerController对4.1.x的先前版本或同等版本工作正常我几乎解决了播放视频的问题... 以下是代码,
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *strURL = @"http://iphonetv.orange.mu:1935/live/ndtvgtimes.stream/playlists.m3u8";
NSURLRequest *urlReq = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:strMovieURL]];
[myWebView loadRequest:urlReq];
}
- (BOOL)webView:(UIWebView *)myWebView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if ([[[url path] pathExtension] isEqualToString:@"mp4" ] || [[[url path] pathExtension] isEqualToString:@"m4v" ] || [[[url path] pathExtension] isEqualToString:@"m3u8" ])
{
//video files are .mp4 or m4v, stream indexes are m3u8
//it's a movie, go play!
[self playMovieAtURL:url];
return YES;
}
else
{
return [super webView:myWebView shouldStartLoadWithRequest:request navigationType:navigationType];
}
}
-(void)playMovieAtURL:(NSURL*)theURL
{
theMovie = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
theMovie.scalingMode = MPMovieScalingModeAspectFit;
[[NSNotificationCenter defaultCenter] addObserver:self selector: @selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie play];
}
-(void)myMovieFinishedCallback:(NSNotification*)aNotification
{
MPMoviePlayerController* theMovie1 = [aNotification object];
//theMovie = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie1];
[theMovie stop];
float version = [[[UIDevice currentDevice] systemVersion] floatValue];
if (version >= 3.0)
{
// iPhone 3.0 code here
// theMovie.initialPlaybackTime = -1.0; //Breaks on 2.x compiling
[theMovie setInitialPlaybackTime: -1.0]; //Only gives a warning on 2.x :)
}
[theMovie release];
NSLog(@"Go STOP received");
}