2011-03-17 130 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