为什么不能多次使用MPMoviePlayerController?

时间:2009-10-10 17:08:18

标签: ios xamarin.ios mono mpmovieplayercontroller

在MonoTouch中,我们遇到了电影播放器​​样本的问题,因为它只会播放一次视频,但不会再播放一次。

我问这个问题是为了回答问题,因为它已经打击了各种各样的人。

3 个答案:

答案 0 :(得分:17)

MPMoviePlayerController是引擎盖下的单身人士。如果您没有正确释放(ObjC)或Dispose()'d(MonoTouch)并且您创建了第二个实例,它将无法播放或仅播放音频。

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

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

这就是你想要在控制器上调用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 (); });

答案 1 :(得分: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]; 
} 

答案 2 :(得分: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];
}