3

我只是使用的MPMoviePlayerController播放視頻......我的代碼是的MPMoviePlayerController縮放模式問題

-(void)playMovie:(NSURL *)url 
{ 
    moviePlayer = 
    [[MPMoviePlayerController alloc] 
    initWithContentURL:url]; 
    if (IDIOM==IPAD) { 
     [moviePlayer.view setFrame:CGRectMake(22,100, 720, 300)]; 
    } 
    else 
    { 
     (IS_IPHONE_5)? [moviePlayer.view setFrame:CGRectMake(22, 70, 280, 150)]:[moviePlayer.view setFrame:CGRectMake(22, 40, 260, 140)]; 
    } 
    [_scrollView addSubview:moviePlayer.view]; 
    moviePlayer.scalingMode =MPMovieScalingModeFill; 
    [moviePlayer prepareToPlay]; 
    [moviePlayer play]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidEnterFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:Nil]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerDidExitFullScreen:) name:MPMoviePlayerDidExitFullscreenNotification object:Nil]; 

} 

-(void)moviePlayerDidEnterFullscreen :(id)sender 
{ 
    NSLog(@"fullscreen"); 
    [moviePlayer play]; 
    moviePlayer.scalingMode =MPMovieScalingModeFill; 

} 

- (void) moviePlayerDidExitFullScreen:(id)sender { 

    NSLog(@"exit full screen"); 
    [moviePlayer play]; 
    moviePlayer.scalingMode =MPMovieScalingModeFill; 

} 

這裏的時候,我開始播放視頻將在「MPMovieScalingModeFill」模式......但我的問題是,如果我按全屏幕,它顯示全屏幕視頻..當我按退出「全屏」,然後我的視頻模式進入「MPMovieScalingModeAspectFit」mode.but我需要始終在「MPMovieScalingModeFill」模式。whats錯誤與我的代碼..請幫助我...

+0

你不應該在全屏通知內觸發'play'。 – Till

+0

但它停止播放,所以我在全屏幕通知中再次呼叫該遊戲 – jafar

+0

這完全沒有意義。從屏幕切換到全屏時,不應停止播放。你的代碼中必須有一些東西,你沒有告訴我們停止播放。我的猜測是,在「viewWill/DidAppear」/「viewWill/DidDisappear」中會出現一些內容 - 當全屏模式完全隱藏viewController的視圖時,會在離開或進入全屏時觸發這些外觀通知。 – Till

回答

3

我相信這會產生MPMoviePlayerScalingModeDidChangeNotification

[[NSNotificationCenter defaultCenter] addObserver:self 
       selector:@selector(movieScalingModeDidChange:) 
       name:MPMoviePlayerScalingModeDidChangeNotification 
       object:nil]; 

來源:Apple Doc

MPMoviePlayerScalingModeDidChangeNotification

當電影播放器​​的縮放模式已經改變了發佈。沒有userInfo字典。 縮放模式可以通過編程或通過用戶交互進行更改。要設置或檢索電影播放器​​的縮放模式,請訪問其scalingMode屬性。其狀態已更改的電影播放器​​可用作與通知關聯的對象。

1

這不是「理想」的解決方案,但它的工作原理!基本上,一旦你退出全屏,MPMoviePlayerController實例全部搞砸了,重新設置縮放屬性爲MPMovieScalingModeFill不會幫助,無論你在哪裏或何時做(我已經嘗試了各種各樣的東西,一小時後放棄)。最簡單的解決方法是刪除了的MPMoviePlayerController,只是分配的MPMoviePlayerController的新實例每次滿屏退出時間(不理想,但完全適用):

- (void)viewDidAppear:(BOOL)animated 
{ 
    [super viewDidAppear:NO]; 
    if (self.moviePlayer != nil) 
     [self.moviePlayer.view removeFromSuperview]; 
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:self.videoURL]; 
    self.moviePlayer.view.frame = CGRectMake(#, #, #, #); 
    self.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
    self.moviePlayer.shouldAutoplay = NO; 
    [self.moviePlayer setContentURL:self.videoURL]; 
    [self.moviePlayer prepareToPlay]; 
    [self.moviePlayer setScalingMode:MPMovieScalingModeFill]; 
    [self.view addSubview:self.moviePlayer.view]; 
} 

PS:不要忘記調用超在viewDidAppear或遭受不可預見的混亂的種種(在iOS開發一個非常常見的錯誤)

3

首先設置ScalingMode到無,然後ScalingMode設置爲AspectFill

Swift代碼:

NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayerExitFullscreen:", name: MPMoviePlayerDidExitFullscreenNotification, object: self.moviePlayer) 
NSNotificationCenter.defaultCenter().addObserver(self, selector: "moviePlayerEnterFullscreen:", name: MPMoviePlayerWillEnterFullscreenNotification, object: self.moviePlayer) 

func moviePlayerEnterFullscreen (notification : NSNotification) 
{ 
    self.moviePlayer.scalingMode = MPMovieScalingMode.None 
    self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill 
} 

func moviePlayerExitFullscreen (notification : NSNotification) 
{ 
    self.moviePlayer.scalingMode = MPMovieScalingMode.None 
    self.moviePlayer.scalingMode = MPMovieScalingMode.AspectFill 
}