12

在 MonoTouch 中,我们在 Movie Player 示例中遇到了这个问题,因为它只会播放视频一次,但不会播放第二次。

我问这个问题是为了发布答案,因为它已经影响了很多人。

4

3 回答 3

17

MPMoviePlayerController 是底层的单例。如果您没有正确释放 (ObjC) 或 Dispose()'d (MonoTouch),并且您创建了第二个实例,它要么不播放,要么只播放音频。

此外,如果您订阅了 MPMoviePlayerScalingModeDidChangeNotification 或 MPMoviePlayerPlaybackDidFinishNotification 或 MPMoviePlayerContentPreloadDidFinishNotification,请注意发布的 NSNotification 也会引用 MPMoviePlayerController,因此如果您保留它,您将引用播放器。

尽管 Mono 的垃圾收集器最终会启动,但这是需要确定性终止的情况(您希望引用现在消失,而不是在 GC 决定执行收集时消失)。

这就是为什么要在控制器上调用 Dispose() 方法,在通知上调用 Dispose() 方法的原因。

例如:

// Deterministic termination, do not wait for the GC
if (moviePlayer != null){
    moviePlayer.Dispose ()
    moviePlayer = null;
}

如果您正在收听通知,请在最后调用通知处理程序中的 Dispose,以释放它保留给 MPMoviePlayerController 的引用,例如:

var center = NSNotificationCenter.DefaultCenter;
center.AddObserver (
    "MPMoviePlayerPlaybackDidFinishNotification"),
    (notify) => { Console.WriteLine ("Done!"); notify.Dispose (); });
于 2009-10-10T17:13:07.357 回答
1

看不到您的代码 Nir,我没有编辑权限,所以又是这样:

秘密在于 endPlay 设置:moviePlayer.initialPlaybackTime = -1; 在释放它之前。试试看 : :)

-(void)playMovie:(NSString *)urlString{ movieURL = [NSURL URLWithString:urlString];          
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];    
moviePlayer.initialPlaybackTime = 0; 
//Register to receive a notification when the movie has finished playing. 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endPlay:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 
moviePlayer.movieControlMode = MPMovieControlModeDefault;
moviePlayer.backgroundColor = [UIColor blackColor];

[moviePlayer play];

}

-(void)endPlay: (NSNotification*)notification{
 [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; 
moviePlayer.initialPlaybackTime = -1; 
[moviePlayer stop]; 
[moviePlayer release]; 
} 
于 2010-02-09T19:35:56.357 回答
0

秘密在于 endPlay 设置:moviePlayer.initialPlaybackTime = -1; 在释放它之前。试试看 : :)

-(void)playMovie:(NSString *)urlString{
    movieURL = [NSURL URLWithString:urlString]; 
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    moviePlayer.initialPlaybackTime = 0;
    //Register to receive a notification when the movie has finished playing. 
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(endPlay:) 
                                                 name:MPMoviePlayerPlaybackDidFinishNotification 
                                               object:moviePlayer];

    moviePlayer.scalingMode = MPMovieScalingModeAspectFit; 
    moviePlayer.movieControlMode = MPMovieControlModeDefault;
    moviePlayer.backgroundColor = [UIColor blackColor];

    [moviePlayer play];

}

-(void)endPlay: (NSNotification*)notification{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
    moviePlayer.initialPlaybackTime = -1;
    [moviePlayer stop];
    [moviePlayer release];
}
于 2010-02-04T12:35:45.500 回答