為您的 iOS 應用程式新增進階功能

廣告插播時間點

iOS Sender SDK 支援在 針對特定媒體串流。

詳情請參閱 網路接收端廣告插播總覽瞭解詳情 瞭解廣告插播的運作方式

雖然你可以在傳送端和接收端中指定休息時間,但建議設定 「網路接收端」Android TV 接收器,保持一致 跨平台上的行為

在 iOS 上,使用 GCKAdBreakClipInfo敬上 和 GCKAdBreakInfo

Swift
,瞭解如何調查及移除這項存取權。
let breakClip1Builder = GCKAdBreakClipInfoBuilder(adBreakClipID: "bc0")
breakClip1Builder.title = "Clip title"
if let posterUrl = URL(string: "https://www.some.url") {
  breakClip1Builder.posterURL = posterUrl
}
breakClip1Builder.duration = 60
breakClip1Builder.whenSkippable = 5  // Set this field so that the ad is skippable
let breakClip1 = breakClip1Builder.build()

let breakClip2 = ...
let breakClip3 = ...


let break1 = GCKAdBreakInfoBuilder(adBreakID: "b0", adBreakClipIds: ["bc0", "bc1", "bc2"]).build()
let mediaInfoBuilder = GCKMediaInformationBuilder(entity: "entity")
...
mediaInfoBuilder.adBreaks = [break1]
mediaInfoBuilder.adBreakClips = [breakClip1, breakClip2, breakClip3]
...
mediaInformation = mediaInfoBuilder.build()

let mediaLoadRequestDataBuilder = GCKMediaLoadRequestDataBuilder()
mediaLoadRequestDataBuilder.mediaInformation = mediaInformation

sessionManager.currentSession?.remoteMediaClient?.loadMedia(with: mediaLoadRequestDataBuilder.build())
Objective-C
GCKAdBreakClipInfoBuilder *breakClipInfoBuilder = [[GCKAdBreakClipInfoBuilder alloc] initWithAdBreakClipID:@"bc0"];
breakClipInfoBuilder.title = @"Clip title";
breakClipInfoBuilder.posterURL = [[NSURL alloc] initWithString:@"https://www.some.url"];
breakClipInfoBuilder.duration = 60;
breakClipInfoBuilder.whenSkippable = 5;
GCKAdBreakClipInfo *breakClip1 = breakClipInfoBuilder.build;

GCKAdBreakClipInfo *breakClip2 = ...
GCKAdBreakClipInfo *breakClip3 = ...

GCKAdBreakInfo *break1 = [[GCKAdBreakInfoBuilder alloc] initWithAdBreakID:@"b0"
                                                           adBreakClipIds:@[@"bc0", @"bc1", @"bc2"]].build;

GCKMediaInformationBuilder *mediaInfoBuilder = [[GCKMediaInformationBuilder alloc]
                                                initWithEntity:@"entity"];
...
mediaInfoBuilder.adBreaks = @[break1];
mediaInfoBuilder.adBreakClips = @[breakClip1, breakClip2, breakClip3];
...
self.mediaInformation = [mediaInfoBuilder build];

GCKMediaLoadRequestDataBuilder *mediaLoadRequestDataBuilder = [[GCKMediaLoadRequestDataBuilder alloc] init];
mediaLoadRequestDataBuilder.mediaInformation = self.mediaInformation;

// Send a load request to the remote media client.
GCKRequest *request = [self.sessionManager.currentSession.remoteMediaClient
                                loadMediaWithLoadRequestData:[mediaLoadRequestDataBuilder build]];

可變播放速率

您的應用程式可以顯示和變更目前媒體項目的播放速率。 您可以使用 -[setPlaybackRate:] 或 第 -[setPlaybackRate:customData:]GCKRemoteMediaClient、 使用 GCKUIPlaybackRateControllerplaybackRateController GCKUIMediaController, 並使用 playbackRateGCKUIPlaybackRateController

程式碼範例

以下兩個檔案會實作 GCKUIPlaybackRateController,以便控制系統 將播放率區隔成「正常」、「半速」和 「兩倍速」按鈕:

Swift
,瞭解如何調查及移除這項存取權。
import GoogleCast

/**
 * An implementation of GCKUIPlaybackRateController that controls playback rate
 * using a segmented control that has "normal", "half speed", and "double speed"
 * buttons.
 */
class SegmentedButtonPlaybackRateController: GCKUIPlaybackRateController {
  static let kSegmentNormal = 0;
  static let kSegmentHalfSpeed = 1;
  static let kSegmentDoubleSpeed = 2;

  var segmentedControl: UISegmentedControl!

  override var playbackRate: Float {
    didSet {
      var buttonIndex = 0

      // Map the playback rate to one of our three supported speeds.
      if playbackRate == 1.0 {
        buttonIndex = SegmentedButtonPlaybackRateController.kSegmentNormal
      } else if playbackRate < 1.0 {
        buttonIndex = SegmentedButtonPlaybackRateController.kSegmentHalfSpeed
      } else {
        buttonIndex = SegmentedButtonPlaybackRateController.kSegmentDoubleSpeed
      }

      segmentedControl?.selectedSegmentIndex = buttonIndex
    }
  }
  override var inputEnabled: Bool {
    didSet {
      segmentedControl?.isEnabled = inputEnabled
    }
  }

  /**
   * Designated initializer.
   *
   * @param segmentedControl The segmented control for changing/displaying the
   * playback rate.
   */
  convenience init(_ segmentedControl: UISegmentedControl) {
    self.init()
    self.segmentedControl = segmentedControl;

    segmentedControl.addTarget(self,
                               action: #selector(segmentedControlTapped(sender:)),
                               for: UIControl.Event.valueChanged)
  }

  @objc func segmentedControlTapped(sender: UISegmentedControl) {
    var playbackRate: Float = 1.0

    switch segmentedControl?.selectedSegmentIndex {
    case SegmentedButtonPlaybackRateController.kSegmentHalfSpeed:
      playbackRate = 0.5;
    case SegmentedButtonPlaybackRateController.kSegmentDoubleSpeed:
      playbackRate = 2.0;
    case SegmentedButtonPlaybackRateController.kSegmentNormal:
      fallthrough
    default:
      playbackRate = 1.0;
    }

    self.playbackRate = playbackRate
  }
}
Objective-C

SegmentedButtonPlaybackRateController.h

#import <GoogleCast/GoogleCast.h>
#import <UIKit/UIKit.h>

/**
 * An implementation of GCKUIPlaybackRateController that controls playback rate
 * using a segmented control that has "normal", "half speed", and "double speed"
 * buttons.
 */
@interface SegmentedButtonPlaybackRateController : GCKUIPlaybackRateController

/**
 * Designated initializer.
 *
 * @param segmentedControl The segmented control for changing/displaying the
 * playback rate.
 */
- (instancetype)initWithSegmentedControl:(UISegmentedControl *)segmentedControl;

@end

SegmentedButtonPlaybackRateController.m

#import "SegmentedButtonPlaybackRateController.h"

@interface SegmentedButtonPlaybackRateController () {
  UISegmentedControl *_segmentedControl;
}

@end

static const NSInteger kSegmentNormal = 0;
static const NSInteger kSegmentHalfSpeed = 1;
static const NSInteger kSegmentDoubleSpeed = 2;

@implementation SegmentedButtonPlaybackRateController

- (instancetype)initWithSegmentedControl:(UISegmentedControl *)segmentedControl {
  if (self = [super init]) {
    _segmentedControl = segmentedControl;
    [_segmentedControl addTarget:self
                          action:@selector(segmentedControlTapped:)
                forControlEvents:UIControlEventValueChanged];
  }
  return self;
}

- (void)setPlaybackRate:(float)playbackRate {
  [super setPlaybackRate:playbackRate];

  NSInteger buttonIndex = 0;

  // Map the playback rate to one of our three supported speeds.
  if (playbackRate == 1.0) {
    buttonIndex = kSegmentNormal;
  } else if (playbackRate < 1.0) {
    buttonIndex = kSegmentHalfSpeed;
  } else {
    buttonIndex = kSegmentDoubleSpeed;
  }

  _segmentedControl.selectedSegmentIndex = buttonIndex;
}

- (void)setInputEnabled:(BOOL)inputEnabled {
  _segmentedControl.enabled = inputEnabled;
  [super setInputEnabled:inputEnabled];
}

- (void)segmentedControlTapped:(id)sender {
  float playbackRate;

  switch (_segmentedControl.selectedSegmentIndex) {
    case kSegmentHalfSpeed:
      playbackRate = 0.5;
      break;

    case kSegmentDoubleSpeed:
      playbackRate = 2.0;
      break;

    case kSegmentNormal:
    default:
      playbackRate = 1.0;
      break;
  }

  self.playbackRate = playbackRate;
}

@end

新增自訂管道

Cast 架構提供兩種方式:建立頻道以傳送自訂訊息 轉換為網路接收端:

  1. GCKCastChannel敬上 主要是用來實作 關聯狀態
  2. GCKGenericChannel敬上 提供做為子類別的替代方法;超過 100 個 Pod 郵件傳送給委派代表,以便從其他地方處理。

以下是 GCKCastChannel 的實作範例:

Swift
,瞭解如何調查及移除這項存取權。
class HGCTextChannel: GCKCastChannel {
  override func didReceiveTextMessage(_ message: String) {
    print("received message: \(message)")
  }
}
Objective-C

HGCTextChannel.h

#import <GoogleCast/GCKCastChannel.h>

@interface HGCTextChannel : GCKCastChannel

@end

HGCTextChannel.m

#import "HGCTextChannel.h"

@implementation HGCTextChannel
- (void)didReceiveTextMessage:(NSString*)message {
  NSLog(@"received message: %@", message);
}

@end

頻道隨時都可以註冊如果工作階段目前不在 連結的狀態,系統就會在 只要頻道的命名空間 網路接收器應用程式中繼資料的支援的命名空間清單。

每個自訂管道都是由獨一無二的命名空間定義,而且必須以 前置字串 urn:x-cast:,例如 urn:x-cast:com.example.custom。是 您就能擁有多個自訂管道,而且每個管道的命名空間都不一樣 Web Receiver 應用程式也可以收發郵件 訊息 使用相同的命名空間

Swift
,瞭解如何調查及移除這項存取權。
var error: GCKError?
let textChannel = HGCTextChannel.init(namespace: "urn:x-cast:com.google.cast.sample.helloworld")
sessionManager.currentCastSession?.add(textChannel)
textChannel.sendTextMessage("Hello World", error: &error)

if error != nil {
  print("Error sending text message \(error.debugDescription)")
}
Objective-C
NSError *error;
HGCTextChannel *textChannel = [[HGCTextChannel alloc] initWithNamespace:@"urn:x-cast:com.google.cast.sample.helloworld"];
[sessionManager.currentCastSession addChannel:textChannel];
[textChannel sendTextMessage:@"Hello World"
                       error:&error];

if (error != nil) {
  NSLog(@"Error sending text message: %@", error);
}

提供需要在特定管道 已連線或中斷連線,請覆寫 -[didConnect]-[didDisconnect] 方法 (如果使用 GCKCastChannel,或 為 -[castChannelDidConnect:]-[castChannelDidDisconnect:] 方法的 GCKGenericChannelDelegate 如果使用 GCKGenericChannel

支援自動播放

請參閱自動播放和將 API 排入佇列

覆寫圖片選取及快取設定

架構的各種元件 (例如 Cast 對話方塊、小型應用程式) 展開的控制器和 GCKUIMediaController敬上 若已設定此項),將顯示目前投放媒體的圖片。網址 通常都會包含在 GCKMediaMetadata敬上 但傳送方應用程式可能有網址的替代來源。

GCKUIImagePicker敬上 「通訊協定」會定義針對特定用途選取適當映像檔的方式 然後根據所需大小包括單一方法 -[getImageWithHints:fromMetadata:] 該程序會 GCKUIImageHints 物件 GCKMediaMetadata 物件做為參數,然後傳回 GCKImage 物件做為 結果。架構提供預設實作 GCKUIImagePicker 一律會選取圖片清單中的第一張圖片 GCKMediaMetadata 物件,但應用程式可提供替代 方法是設定 imagePicker 屬性的 GCKCastContext 單例模式

GCKUIImageCache敬上 通訊協定也會定義快取映像檔 透過 HTTPS 處理工作流程架構提供預設實作 GCKUIImageCache,會將下載的圖片檔儲存在應用程式快取中 但應用程式可透過設定 imageCache 屬性 GCKCastContext敬上 單例模式

後續步驟

以上就是可用於 iOS 寄件者應用程式的功能。 現在可以為其他平台建構傳送者應用程式 (Android網頁)、 或建立網路接收端