2013-05-14 33 views
1

所以我試着去演一部電影是這樣的:目標C - 的MPMoviePlayerController削減了

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    vidLocation=[[NSURL alloc] initWithString:@"http://cdn.webwaterfalls.com/uni/introNew2.mp4"]; 

    UIView * vidView=[[UIView alloc] initWithFrame:CGRectMake(10, 10, 300, 300)]; 

    [self.view addSubview:vidView]; 

    MPMoviePlayerController * vid=[[MPMoviePlayerController alloc] initWithContentURL:vidLocation]; 

    if(vid){ 

     [vid setContentURL:vidLocation]; 

     [vid setMovieSourceType:MPMovieSourceTypeStreaming]; 

     [vid.view setFrame: vidView.bounds]; 

     [vidView addSubview:vid.view]; 

     [vid play]; 

    } 
} 

,電影開始播放時,顯示所有的暫停,尋求,體積和全屏幕控制。

但3秒後電影剛剛切斷,什麼也沒有顯示。沒有電影。沒有控制。

我試過不同的電影(所有這些都在ipad上播放),但他們都在3秒後放棄播放。

任何人都可以解釋我做錯了什麼?

回答

1

嘗試這樣:

- (void)viewDidAppear:(BOOL)animated { 
    [super viewDidAppear:animated]; 

    //Use Apple's sample stream 
    NSURL *mediaURL = [NSURL URLWithString:@"http://cdn.webwaterfalls.com/uni/introNew2.mp4"]; 
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:mediaURL]; 

    //Begin observing the moviePlayer's load state. 
    [[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(moviePlayerLoadStateChanged:) 
              name:MPMoviePlayerLoadStateDidChangeNotification 
              object:self.moviePlayer]; 

    [self.moviePlayer setShouldAutoplay:NO];//Stop it from autoplaying 
    [self.moviePlayer prepareToPlay];//Start preparing the video 
} 

- (void)moviePlayerLoadStateChanged:(NSNotification *)notification{ 
    NSLog(@"State changed to: %d\n", self.moviePlayer.loadState); 
    if(self.moviePlayer.loadState == MPMovieLoadStatePlayable){ 
     //if load state is ready to play 
     [self.view addSubview:[self.moviePlayer view]]; 
     [self.moviePlayer setFullscreen:YES]; 
     [self.moviePlayer play];//play the video 
    } 

}