2012-09-16 58 views
0

我想要在iPhone應用程序中播放基本電影;然而,我似乎無法讓它工作。這裏是我的全部代碼:爲什麼我的電影不能使用MPMoviePlayerController播放?

#import <MediaPlayer/MediaPlayer.h> 
#import "ViewController.h" 

@implementation ViewController 

- (IBAction)playMoviePressed:(id)sender 
{ 
    NSURL *url = [NSURL URLWithString:@"http://www.ebookfrenzy.com/ios_book/movie/movie.mov"]; 

    MPMoviePlayerController *moviePlayer = 
    [[MPMoviePlayerController alloc] initWithContentURL:url]; 

    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePlayBackDidFinish:) 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:moviePlayer]; 

    moviePlayer.controlStyle = MPMovieControlStyleDefault; 
    moviePlayer.shouldAutoplay = YES; 
    [self.view addSubview:moviePlayer.view]; 
    [moviePlayer setFullscreen:YES animated:YES]; 
    [moviePlayer prepareToPlay]; 
    [moviePlayer play]; 
} 


@end 

我在屏幕上一個按鈕,當被竊聽調用playMoviePressed。我知道這個方法正在被調用。我也試過用.mov.mp4這個本地文件,我把它拖入xcode。

回答

3

我收到了你的代碼中加入一幀爲它播放視頻:

[moviePlayer.view setFrame:CGRectMake(0, 0, 320, 200)]; 

然而視頻是波濤洶涌,但可能是我的互聯網連接,但。看到你在使用IBAction,你可能使用了Interface Builder或Storyboard,所以我認爲阻止它播放的唯一原因是你錯過了與IBOutlet的連接。

1

如果你想有一個全屏模式下,你可以簡單地使用MPMoviePlayerViewController和做類似:

MPMoviePlayerViewController* viewController = [[MPMoviePlayerViewController alloc] initWithContentURL:url]; 
[self presentMoviePlayerViewControllerAnimated:viewController]; 
0

我想通了。我的問題真的很愚蠢。因爲我沒有保留一個強指針指向我的MPMoviePlayerViewController,所以只要方法結​​束,它就會被釋放。界面中的一個簡單的@property解決了這個問題。

相關問題