2012-02-24 130 views
3

我想使用MPMoviePlayerController在UIView内播放视频。 按下UIButton后,视频出现并开始播放。 不幸的是,在3-4秒后,视频变黑,音频仍在播放。 任何想法? 感谢您的所有时间。几秒钟后视频播放消失

(使用的Xcode 4.2.1)

-(void) playMovieButtonPressed:(UIButton*) button{ 

     NSString* video = @"testmovie.m4v"; 

     NSString *filepath = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]]; 
     NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
     MPMoviePlayerController* player = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 


     [[NSNotificationCenter defaultCenter] addObserver:self 
               selector:@selector(moviePlaybackComplete:) 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:player]; 

     player.movieSourceType = MPMovieSourceTypeFile; 
     [player.view setFrame:CGRectMake(20, 400, 300, 200)]; 
     [self.view addSubview:player.view]; 
     [player prepareToPlay]; 
     [player play]; 
    } 

- (void) moviePlaybackComplete:(NSNotification*) notification { 
    NSLog(@"videoviewcontroller complete: %@", notification); 

    MPMoviePlayerController *mymoviePlayerController = [notification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:mymoviePlayerController]; 
} 
+0

这听起来很可笑,你有没有试过另一个视频。我的工作代码几乎完全相同..我所有的扩展类型都是mp4,但我没有使用player.movi​​eSourceType – DJPlayer 2012-02-24 14:13:52

+0

Thx进行回复。好,当然。我测试了其他视频,其他格式和其他来源类型 - 没有运气。我也用一个按钮在一个新的项目中测试了它 - 完全一样的问题。你安装了哪个Xcode版本? – geforce 2012-02-24 15:04:18

+0

同一版本4.2.1 – DJPlayer 2012-02-24 15:27:09

回答

7

从来就谈过一个苹果工程师。解决的办法是玩家必须是视图控制器的实例变量或属性。所以当视图被拆除时它仍然可以访问。

-(void) playMovieButtonPressed:(UIButton*) button{ 

     NSString* video = @"testmovie.m4v"; 

     NSString *filepath = [[NSBundle mainBundle] pathForResource:[video stringByDeletingPathExtension] ofType:[video pathExtension]]; 
     NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
     self.moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 

// ..... 
} 
+0

尝试了很多解决方案之后,这变成了正确的解决方案! – 2014-10-31 13:45:57