2011-03-18 80 views
10

如果視頻播放失敗,我想顯示UIAlert。所以我註冊了MPMoviePlayerPlaybackDidFinishNotification爲我的電影播放器​​:如何在MPMoviePlayerController播放失敗時獲取錯誤描述

[NSNotificationCenter defaultCenter]的addObserver:自我選擇:@選擇(myMovieFinishedCallback :)名稱:MPMoviePlayerPlaybackDidFinishNotification對象:self.movi​​ePlayer]。

在我的myMovieFinishedCallback中:我檢查用戶信息字典中是否有一個名爲error的對象。在我的真實設備上,我沒有收到這個錯誤(沒有網絡錯誤,404文件錯誤)。在iPhone模擬器上,我收到錯誤。

當我收到MPMoviePlayerPlaybackDidFinishNotification時,如何正確檢查推理?

回答

18

不幸的是,MPMoviePlayerController(直到但不包括iOS 4.3)從文檔中沒有詳細的問題識別。如果MPMoviePlayerPlaybackDidFinishNotification的UserInfo中存在任何問題,它只會返回MPMovieFinishReasonPlaybackError

對於iOS 4.3,我們終於獲得了包含擴展且非常有用的信息的errorLogaccessLog屬性。 見MPMoviePlayerController Reference

在iOS 5.0中,有一個error密鑰隨設備構建而來,而不僅僅是在模擬器中。那errorNSError的一個實例,並提供非常有用的信息。不幸的是,這還沒有被蘋果公司記錄下來,因此它可能會在任何iOS版本上發生變化。另外,對於給定的錯誤代碼似乎沒有解釋。例如,HTTP-Status:404將導致給定錯誤實例中的錯誤代碼-1100。但是,這將是如何以最合適的方式處理此通知的示例。

[[NSNotificationCenter defaultCenter] addObserver:self 
             selector:@selector(handleMPMoviePlayerPlaybackDidFinish:) 
              name:MPMoviePlayerPlaybackDidFinishNotification 
              object:nil]; 

這將是一個適當的通知處理程序:

- (void)handleMPMoviePlayerPlaybackDidFinish:(NSNotification *)notification 
{ 
    NSDictionary *notificationUserInfo = [notification userInfo]; 
    NSNumber *resultValue = [notificationUserInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey]; 
    MPMovieFinishReason reason = [resultValue intValue]; 
    if (reason == MPMovieFinishReasonPlaybackError) 
    { 
     NSError *mediaPlayerError = [notificationUserInfo objectForKey:@"error"]; 
     if (mediaPlayerError) 
     { 
      NSLog(@"playback failed with error description: %@", [mediaPlayerError localizedDescription]); 
     } 
     else 
     { 
      NSLog(@"playback failed without any given reason"); 
     } 
    } 
} 

最後但並非最不重要的,不要忘記釋放你要處理它的對象的實例時,從默認中心刪除通知處理程序中。

[[NSNotificationCenter defaultCenter] removeObserver:self 
               name:MPMoviePlayerPlaybackDidFinishNotification 
               object:nil]; 
+0

這是可愛的,但它似乎已經壞了。使用iOS 8.1和我的MPMoviePlayer.url得到404錯誤,但它沒有觸發[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish :) name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; – Lombas 2016-07-12 20:23:40

+1

鑑於蘋果已經棄用MPMoviePlayer,我覺得使用AVFoundation代替它會更安全。 – Till 2016-07-13 20:11:32

0

你可以看一下背後的userinfo字典的MPMoviePlayerPlaybackDidFinishReasonUserInfoKey價值。如果值爲MPMovieFinishReasonPlaybackError,則可以假定有些內容出錯。這種方法相當可靠。

2

我有同樣的問題。雖然我註冊了MPMoviePlayerLoadStateDidChangeNotification回調函數,並從userInfo變量中獲取錯誤,但沒有顯示錯誤。我花了很多時間在論壇上搜索,在玩過代碼後,我意識到問題出在哪裏。

首先,你需要註冊回調函數:

// Register that the did finish notification (movie stopped) 
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer]; 

內MovieFinished回調函數,你將需要:

NSDictionary *notice = [paramNotification userInfo]; 

if (notice != nil) 
{ 
    NSError *errorInfo = [notice objectForKey:@"error"]; 

    if (errorInfo != nil) { 
     UIAlertView *notice = [[UIAlertView alloc] initWithTitle:@"Error" message:[errorInfo localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil]; 
     [notice show]; 
     [notice release];    
    } 
} 

該代碼會顯示相關的moviecontroller任何錯誤。所以..我的代碼中有什麼問題?..我在錯誤的地方使用[moviecontroller play]方法,所以請檢查你的方法。

祝你好運!

+0

謝謝你,我看到了這個片段的錯誤信息。 – 2012-10-19 08:06:26