Objective C - MPMoviePlayerController被删除

时间:2013-05-14 10:10:01

标签: objective-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 个答案:

答案 0 :(得分: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
    }

}