2013-06-06 49 views
-2

當ARC打開時,我無法播放視頻(視頻永久加載)。當ARC關閉時,我可以成功播放視頻。我不知道爲什麼以及如何編輯我的代碼與ARC on.Here播放視頻是我的代碼ARC使播放視頻失敗

-(IBAction)playMovie:(id)sender 
{ 
    NSString *filepath = [[NSBundle mainBundle] pathForResource:@"big-buck-bunny-clip" ofType:@"m4v"]; 
    NSURL *fileURL = [NSURL fileURLWithPath:filepath]; 
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 
    [[NSNotificationCenter defaultCenter] addObserver:self 
              selector:@selector(moviePlaybackComplete:) 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:moviePlayerController]; 
    [self.view addSubview:moviePlayerController.view]; 
    moviePlayerController.fullscreen = YES; 
    [moviePlayerController play]; 
} 
- (void)moviePlaybackComplete:(NSNotification *)notification 
{ 
    MPMoviePlayerController *moviePlayerController = [notification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self 
                name:MPMoviePlayerPlaybackDidFinishNotification 
                object:moviePlayerController]; 
    [moviePlayerController.view removeFromSuperview]; 

} 
+3

你需要一個參考存到自己的MPMoviePlayerController或將在「的playMovie」年底被釋放 – borrrden

+0

見borrrden的評論和使用搜索功能...此問題已在SO上多次提出並解答。例如http://stackoverflow.com/questions/11365791/mpmovieplayercontroller-not-playing-any-video-content-due-to-arc-and-memory-man – Till

回答

0

有幾種方法:

  1. 將移動到的MPMoviePlayerController視圖委託,而不是一個局部變量。 (這是最好的方法)
  2. 使用CFRetain()來手動保留它(這個CoreFoundation函數將在Objective-C對象上完成它的工作而不會觸發ARC錯誤,請記住一旦完成它的工作就立即對其進行修改,並且CFRelease()它)
  3. 轉到項目設置並用編譯器標記-fno-objc-arc標記此文件。 (這會讓你的代碼泄漏內存像地獄。)