当前位置 : 主页 > 手机开发 > ios >

ios – 视频播放几秒钟后消失

来源:互联网 收集:自由互联 发布时间:2021-06-11
我想使用MPMoviePlayerController在UIView中播放视频. 按下UIButton后,视频出现并开始播放. 不幸的是,3-4秒后,视频变黑,音频仍在播放. 有任何想法吗? 谢谢你所有的时间. (使用Xcode 4.2.1) -(void)
我想使用MPMoviePlayerController在UIView中播放视频.
按下UIButton后,视频出现并开始播放.
不幸的是,3-4秒后,视频变黑,音频仍在播放.
有任何想法吗?
谢谢你所有的时间.

(使用Xcode 4.2.1)

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

        NSString* video = @"testmovie.m4v";

        NSString *filepath   =   [[NSBundle mainBundle] pathForResource: ofType:];
        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];  
}
我和一位Apple工程师谈过了.解决方案是播放器必须是视图控制器的实例变量或属性.所以当视图被拆除时它仍然可以访问.

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

        NSString* video = @"testmovie.m4v";

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

// .....
}
网友评论