在iphone上播放视频

时间:2010-09-20 12:43:28

标签: iphone

我的申请中有一个奇怪的问题。我按下UI中的按钮后试图在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];

}

2 个答案:

答案 0 :(得分:0)

您应该在开始播放时将theMovie.view添加到视图层次结构中,并在播放完成后再次将其删除。

答案 1 :(得分:0)

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

你需要添加[self presentMoviePlayerViewControllerAnimated:theMovie]; ..