2011-03-17 114 views
3

我想在我的iPhone应用中播放短片。当我使用下面的代码时,我只听到音频,并看到应用程序的常规视图 。我希望视频能够在此视图之上播放。 我能做些什么呢?在iOS应用中播放视频:音频但无图片

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]; 

回答

4

别把MPMoviePlayerControllerMPMoviePlayerViewController。当您使用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:视频(通常为全屏视频)。

+0

谢谢您的回答。尝试用您的建议替换MPMoviePlayerController部件,但不断收到错误... – user664142 2011-03-17 11:26:37

+0

以下是我所做的: – user664142 2011-03-17 11:30:35

0
MPMoviePlayerController *player = 
     [[MPMoviePlayerController alloc] initWithContentURL: myURL]; 
[player.view.frame = self.view.frame]; 
[self.view addSubview: player.view]; 
// ... 
[player play]; 
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; 

More Details