1

所以我试图播放这样的电影:

- (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 秒后放弃播放。

谁能解释我做错了什么?

4

1 回答 1

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
    }

}
于 2013-05-14T15:31:18.373 回答