使用案例來刪除和取代廣告

以下是刪除和取代廣告的使用案例:

標籤範圍

若要實作PTTimeRangeCollection並將內容範圍標示為廣告:

  1. 準備PTTimeRangeCollection

  2. PTTimeRangeCollection的類型設定為PTTimeRangeCollectionTypeMarkRanges

    此步驟會通知TVSDK自訂範圍必須視為廣告。

    #define PSDK_TIMESCALE 100000 
    
    NSArray *ranges =  @[ 
      [PTReplacementRange  
        replacementRangeWithRange:CMTimeRangeMake(CMTimeMakeWithSeconds 
          (0, PSDK_TIMESCALE),CMTimeMakeWithSeconds(60, AD_TIMESCALE))], 
      [PTReplacementRange  
        replacementRangeWithRange:CMTimeRangeMake(CMTimeMakeWithSeconds 
          (120, PSDK_TIMESCALE),CMTimeMakeWithSeconds(60, AD_TIMESCALE))] 
    ]; 
    
    PTTimeRangeCollection *timeRangeCollection =  
      [[PTTimeRangeCollection alloc] initWithRanges:ranges  
        type:PTTimeRangeCollectionTypeMarkRanges];
    
  3. 建立PTAdMetadata並設定PTTimeRangeCollection

    // Create the PTPlayerItem metadata 
    PTMetadata *metadata = [[PTMetadata alloc] init]; 
    
    // Create the Ad metadata 
    PTAuditudeMetadata *adMetadata = [[PTAuditudeMetadata alloc] init]; 
    adMetadata.timeRangeCollection = timerangeCollection; 
    
    //Set Ad metadata 
    [metadata setMetadata:adMetadata forKey:PTAdResolvingMetadataKey]; 
    
    //Create PTMediaPlayerItem 
    PTMediaPlayerItem *playerItem = [[[PTMediaPlayerItem alloc] initWithUrl:mediaUrl 
                                                                    mediaId:mediaId 
                                                                   metadata:metadata];
    
  4. 建立播放器並開始播放。

    //Create PTMediaPlayer using the created PTMediaPlayer 
    PTMediaPlayer *player = [PTMediaPlayer playerWithMediaPlayerItem:playerItem]; 
    
    //Add player to the player UIView 
    [self.playerView addSubview:(UIView *)player.view]; 
    
    //Start playback 
    [player play];
    

取代範圍

若要實作PTTimeRangeCollection並將內容範圍刪除為廣告:

  1. 準備PTTimeRangeCollection

  2. PTTimeRangeCollection的類型設定為PTTimeRangeCollectionTypeReplaceRanges

    此步驟會通知TVSDK必須以替代內容(廣告)來取代提供的範圍。

    #define PSDK_TIMESCALE 100000 
    
    NSArray *ranges =  @[ 
      [PTReplacementRange replacementRangeWithRange:CMTimeRangeMake(CMTimeMakeWithSeconds 
        (0, PSDK_TIMESCALE),CMTimeMakeWithSeconds(60, AD_TIMESCALE))  
        replacementDuration:CMTimeMakeWithSeconds(30, AD_TIMESCALE)], 
      [PTReplacementRange replacementRangeWithRange:CMTimeRangeMake(CMTimeMakeWithSeconds 
        (120, PSDK_TIMESCALE),CMTimeMakeWithSeconds(60, AD_TIMESCALE))  
        replacementDuration:CMTimeMakeWithSeconds(30, AD_TIMESCALE)] 
                        ]; 
    
    PTTimeRangeCollection *timeRangeCollection =  
      [[PTTimeRangeCollection alloc] initWithRanges:ranges  
                                               type:PTTimeRangeCollectionTypeReplaceRanges];
    
    秘訣

    引數replacementDuration是可選的。 如果未定義,AdServer會決定廣告插播的持續時間。

  3. 建立PTAdMetadata並設定PTTimeRangeCollection

    //Create the PTPlayerItem metadata 
    PTMetadata *metadata = [[PTMetadata alloc] init]; 
    
    //Create the Ad metadata 
    PTAuditudeMetadata *adMetadata = [[PTAuditudeMetadata alloc] init]; 
    adMetadata.timeRangeCollection = timerangeCollection; 
    adMetadata.zoneId = adZoneId; 
    adMetadata.domain = adDomain; 
    adMetadata.signalingMode = PTAdSignalingModeCustomRanges; 
    
    //Set Ad metadata 
    [metadata setMetadata:adMetadata forKey:PTAdResolvingMetadataKey]; 
    
    //Create PTMediaPlayerItem 
    PTMediaPlayerItem *playerItem = [[[PTMediaPlayerItem alloc] initWithUrl:mediaUrl 
                                                                    mediaId:mediaId 
                                                                   metadata:metadata];
    
    秘訣

    儘管signalingMode設定為PTAdSignalingModeCustomRanges,但在設定PTTimeRangeCollectionTypeReplace類型的PTTimeRangeCollection時會自動設定此廣告信令模式。

  4. 建立播放器並開始播放。

    //Create PTMediaPlayer using the created PTMediaPlayer 
    PTMediaPlayer *player = [PTMediaPlayer playerWithMediaPlayerItem:playerItem]; 
    
    //Add player to the player UIView 
    [self.playerView addSubview:(UIView *)player.view]; 
    
    //Start playback 
    [player play];
    

刪除範圍

若要實作PTTimeRangeCollection並將內容範圍刪除為廣告:

  1. 準備PTTimeRangeCollection

  2. PTTimeRangeCollection的類型設為PTTimeRangeCollectionTypeDeleteRanges,通知TVSDK必須刪除提供的範圍。

    #define PSDK_TIMESCALE 100000 
    
    NSArray *ranges =  @[ 
      [PTReplacementRange replacementRangeWithRange:CMTimeRangeMake(CMTimeMakeWithSeconds 
        (0, PSDK_TIMESCALE),CMTimeMakeWithSeconds(60, AD_TIMESCALE))], 
      [PTReplacementRange replacementRangeWithRange:CMTimeRangeMake(CMTimeMakeWithSeconds 
        (120, PSDK_TIMESCALE),CMTimeMakeWithSeconds(60, AD_TIMESCALE))] 
    ]; 
    
    PTTimeRangeCollection *timeRangeCollection =  
      [[PTTimeRangeCollection alloc] initWithRanges:ranges  
                                               type:PTTimeRangeCollectionTypeDeleteRanges];
    
  3. 建立PTAdMetadata並設定PTTimeRangeCollection

    //Create the PTPlayerItem metadata 
    PTMetadata *metadata = [[PTMetadata alloc] init]; 
    
    //Create the Ad metadata 
    PTAuditudeMetadata *adMetadata = [[PTAuditudeMetadata alloc] init]; 
    adMetadata.timeRangeCollection = timerangeCollection; 
    adMetadata.zoneId = adZoneId; 
    adMetadata.domain = adDomain; 
    adMetadata.signalingMode = PTAdSignalingModeServerMap; 
    
    //Set Ad metadata 
    [metadata setMetadata:adMetadata forKey:PTAdResolvingMetadataKey]; 
    
    //Create PTMediaPlayerItem 
    PTMediaPlayerItem *playerItem = [[[PTMediaPlayerItem alloc] initWithUrl:mediaUrl 
                                                                    mediaId:mediaId 
                                                                   metadata:metadata];
    
    秘訣

    廣告插入是在根據PTAdMetadata和目前PTAdSignalingMode刪除自訂範圍後進行。

  4. 建立播放器並開始播放。

    //Create PTMediaPlayer using the created PTMediaPlayer 
    PTMediaPlayer *player = [PTMediaPlayer playerWithMediaPlayerItem:playerItem]; 
    
    //Add player to the player UIView 
    [self.playerView addSubview:(UIView *)player.view]; 
    
    //Start playback 
    [player play];
    

本頁內容