【问题标题】:Why is it not possible to use the MPMoviePlayerController more than once?为什么不能多次使用 MPMoviePlayerController?
【发布时间】:2009-10-10 17:08:17
【问题描述】:

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

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

【问题讨论】:

    标签: ios xamarin.ios mono mpmovieplayercontroller


    【解决方案1】:

    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 (); });
    

    【讨论】:

      【解决方案2】:

      看不到你的代码 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]; 
      } 
      

      【讨论】:

        【解决方案3】:

        秘密在于 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];
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多