PTNotification物件提供播放器狀態、警告和錯誤變更的相關資訊。 停止播放視訊的錯誤也會導致播放器狀態變更。
您的應用程式可以擷取通知和狀態資訊。 您也可以使用通知資訊建立診斷和驗證的記錄系統。
TVSDK也使用*notification
來參考NSNotifications
(PTMediaPlayer
通知)event
*通知,以提供播放器活動的相關資訊。
TVSDK在發問PTNotification
時也會發問PTMediaPlayerNewNotificationItemEntryNotification
。
您實作事件監聽器以擷取並回應事件。 許多事件都提供PTNotification
狀態通知。
PTNotification提供與播放器狀態相關的資訊。
TVSDK提供PTNotification
通知的時間清單。 每個通知都包含下列資訊:
時間戳記
包含下列元素的診斷中繼資料:
type
:資訊、警告或錯誤。
code
:通知的數字表示。
name
:通知的可人讀描述,例如SEEK_ERROR
metadata
:包含通知相關資訊的金鑰/值配對。例如,名為URL
的索引鍵提供與通知相關的URL值。
innerNotification
:直接影響此通 PTNotification
知的對其它對象的引用。
您可以將這些資訊儲存在本機以供日後分析,或將它傳送至遠端伺服器以進行記錄和圖形表示。
TVSDK會針對基本通知設定播放器,不過您必須完成自訂通知的相同設定。
PTNotification
有兩種實現:
PTNotificationHistory
若要監聽通知,TVSDK會執行個體化PTNotification
類別,並將它附加至附加至PTMediaPlayer例項的PTMediaPlayerItem
例項。 每個PTMediaPlayer
只有一個PTNotificationHistory
實例。
如果您要新增自訂,您的應用程式(而非TVSDK)必須執行這些步驟。
監聽PTMediaPlayer
中PTNotification
通知有兩種方式:
使用計時器手動檢查PTMediaPlayerItem
的PTNotificationHistory
並檢查差異:
//Access to the PTMediaPlayerItem
PTMediaPlayerItem *item = self.player.currentItem;
PTNotificationHistory *notificationHistory = item.notificationHistory;
//Get the list of notification events from the notification History
NSArray *notifications = notificationHistory.notificationItems;
使用PTMediaPlayerPTMediaPlayerNewNotificationEntryAddedNotification
的已發佈NSNotification。
使用您想要收到通知的PTMediaPlayer
例項,註冊至NSNotification
:
//Register to the NSNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onMediaPlayerNotification:)
name:PTMediaPlayerNewNotificationEntryAddedNotification object:self.player];
您可以實作通知回呼。
從NSNotification
使用者資訊取得PTNotification
並使用PTMediaPlayerNotificationKey
讀取其值,以實作通知回呼:
- (void) onMediaPlayerNotification:(NSNotification *) nsnotification {
PTNotification *notification = [nsnotification.userInfo objectForKey:PTMediaPlayerNotificationKey];
NSLog(@"Notification: %@", notification);
}
若要新增自訂通知:
建立新的PTNotification
,並使用目前的PTMediaPlayerItem
將其新增至PTNotificationHistory
:
//Access to the PTMediaPlayerItem
PTMediaPlayerItem *item = self.player.currentItem;
PTNotificationHistory *notificationHistory = item.notificationHistory;
//Create notification
PTNotification* notification = [[PTNotification notificationWithType:PTNotificationTypeWarning code:99999 description:@"Custom notification description"];
//Add notification
[notificationHistory addNotification:notification];