2013-10-29 124 views
0

錯誤的電影,我成立了電影播放這樣的:不能暫停循環,因爲的MPMoviePlayerController

self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:@"whatever.mp4"]; 
    self.moviePlayer.controlStyle = MPMovieControlStyleNone; 
    self.moviePlayer.shouldAutoplay = YES; 
    [self.moviePlayer prepareToPlay]; 
    self.moviePlayer.repeatMode = MPMovieRepeatModeOne; 
    self.moviePlayer.view.frame = self.container.bounds; 
    self.moviePlayer.view.userInteractionEnabled = NO; 
    [self.container addSubview:self.moviePlayer.view]; 

    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(moviePlayBackDidFinish:) name: MPMoviePlayerPlaybackStateDidChangeNotification 
               object: self.moviePlayer]; 

的通知是必要的,以保持玩家的循環,因爲REPEATMODE是幾乎無用的(視頻會重複一次或兩次,也許幾次取決於中國大米的價格,然後停止)。所以要保持視頻循環,你必須這樣做:

- (void)moviePlayBackDidFinish:(NSNotification *)note { 
    if (note.object == self.moviePlayer) { 
     NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue]; 
     if (reason == MPMovieFinishReasonPlaybackEnded) { 
      [self.moviePlayer play]; 
     } 
    } 
} 

現在的問題是,我需要能夠暫停視頻。出於某種原因,到

[self.moviePlayer pause]; 

結果通知調用與理由解僱== MPMovieFinishReasonPlaybackEnded即使文檔中明確指出這一點:

Constants 
MPMovieFinishReasonPlaybackEnded 
The end of the movie was reached. 
Available in iOS 3.2 and later. 
Declared in MPMoviePlayerController.h. 

在電影的結尾沒有達到。我只是叫暫停:。所以通知被觸發,因此電影再次播放,否定了暫停動作。

所以你可以看到問題。如何成功暫停循環視頻?

+0

爲什麼不跟蹤通知是由暫停按鈕觸發還是由循環結束使用BOOL變量觸發? – ManicMonkOnMac

+0

如何?就像我說的是視頻結束還是暫停被稱爲MPMovieFinishReasonPlaybackEnded的原因。 – soleil

回答

0

在你的類中創建一個BOOL並初始化爲NO,

@property (nonatomic, assign) BOOL isTriggeredByPause; 

調用

[self.moviePlayer pause]; 

將其值設置之前,

self.isTriggeredByPause = YES; 
在它的方法檢查

(void)moviePlayBackDidFinish:(NSNotification *)note { 
if (note.object == self.moviePlayer) { 
    NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue]; 
    if (reason == MPMovieFinishReasonPlaybackEnded) { 
if(!self.isTriggeredByPause) 
{ 
    [self.moviePlayer play]; 
} 
} 
} 
} 

修改self.isTriggeredByPause在手動播放時的值,否則循環不起作用。

+0

我希望這些類型的黑客是沒有必要的... – soleil