2010-09-20 225 views
0

我在我的应用程序中有一个奇怪的问题。我试图在按下UI中的按钮后在iPhone上播放视频。我的问题是,它仅作为音频播放。和媒体播放器不显示,我只听到声音。 下面是我用来播放视频的方法的代码。在iPhone上播放视频

-(void) playMovieAtURL: (NSURL*) theURL { 

MPMoviePlayerController* theMovie = 

[[MPMoviePlayerController alloc] initWithContentURL: theURL]; 
theMovie.scalingMode = MPMovieScalingModeAspectFill; 

theMovie.movieControlMode = MPMovieControlModeDefault; 

// Register for the playback finished notification 

[[NSNotificationCenter defaultCenter] 

addObserver: self 

selector: @selector(myMovieFinishedCallback:) 

name: MPMoviePlayerPlaybackDidFinishNotification 

object: theMovie]; 

[theMovie play]; 
} 



-(void) myMovieFinishedCallback: (NSNotification*) aNotification 

{ 

MPMoviePlayerController* theMovie = [aNotification object]; 

[[NSNotificationCenter defaultCenter] 

removeObserver: self 

name: MPMoviePlayerPlaybackDidFinishNotification 

object: theMovie]; 

[theMovie release]; 

} 

,这是我如何调用以前的功能发挥设在我的资源视频

-(IBAction)play:(id)sender 
{ 
NSString * url; 
NSURL *theUrl; 

NSBundle *bundle = [NSBundle mainBundle]; 
if (bundle) 
{ 
    NSString *moviePath = [bundle pathForResource:@"240_320_p" ofType:@"mp4"]; 
    if (moviePath) 
    { 
     theUrl = [NSURL fileURLWithPath:moviePath]; 
    } 
} 

[self playMovieAtURL:theUrl]; 

} 

回答

0

当你开始播放,您应该添加theMovie.view到您的视图层次结构和回放后再次将其删除已经完成了。

+0

如何做到这一点?我试过 self.view = theMovie.view; 它的工作,但我认为这不是你的意思 我试过: [self.view addSubView:theMovie.view] ,但也导致声音只有 – belhaouary 2010-09-20 18:03:47

+0

您是否在添加之前正确设置theMovie.view的框架属性它作为子视图? – 2010-09-20 19:41:54

0

完全一样的问题今天早些时候:MPMoviePlayerController worked fine up to iOS 4.0 now just plays sound, no video

您需要添加[自presentMoviePlayerViewControllerAnimated:theMovie]; ..

+0

这导致以下警告 不相容Objective-C的类型的“结构的MPMoviePlayerController *”,传递参数1时的预期“结构MPMoviePlayerViewController *”“presentMoviePlayerViewControllerAnimated:”从不同目标C型 和还我听到的声音,但没有视频出现在屏幕上 – belhaouary 2010-09-20 18:00:59

+0

哦,对不起,没有看到。将您的MPMoviePlayerController更改为MPMoviePLayerViewController – LarsJK 2010-09-21 06:31:16