LoginSignup
9
8

More than 5 years have passed since last update.

iOSでのChromCast対応 ③ 接続先に映像を送ってみる

Posted at

前回まででCast端末への接続は完了しました。
今回は適当な映像を端末へ送ってみます。

関連記事はこちら。

接続先に映像を送ってみる

① 適当な映像を用意する

今回は公式サイトで使っている映像を使用します。
Big Buck Bunny

② 接続完了を受け取る

GCKDeviceManagerDelegateで接続完了を受け取ります

//
// MARK: GCKDeviceManagerDelegate
//
extension ViewController: GCKDeviceManagerDelegate {

    func deviceManager(deviceManager: GCKDeviceManager,
        didConnectToCastApplication applicationMetadata: GCKApplicationMetadata,
        sessionID: String,
        launchedApplication: Bool) {
            print("Application has launched.")
            mediaControlChannel = GCKMediaControlChannel()
            mediaControlChannel?.delegate = self
            deviceManager.addChannel(mediaControlChannel)
            mediaControlChannel?.requestStatus()
            connectDeviceAndSendDate()
    }

③ 映像をCast先に送る

いろいろと設定が面倒なので、公式サイトを参考に、用意した映像データを使って準備してください。

    private func connectDeviceAndSendDate() {
        // Show alert if not connected.
        if (deviceManager?.connectionState != GCKConnectionState.Connected) {
            apeerAlert("Not Connected", message: "Please connect to Cast device")
            return
        }

        // Define Media Metadata.
        let metadata = GCKMediaMetadata()
        metadata.setString("Big Buck Bunny (2008)", forKey: kGCKMetadataKeyTitle)
        metadata.setString("Cast test.", forKey: kGCKMetadataKeySubtitle)

        let url = NSURL(string:"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg")
        metadata.addImage(GCKImage(URL: url, width: 480, height: 360))

        // Define Media Information.
        let mediaInformation = GCKMediaInformation(
            contentID:
            "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
            streamType: GCKMediaStreamType.None,
            contentType: "video/mp4",
            metadata: metadata,
            streamDuration: 0,
            mediaTracks: [],
            textTrackStyle: nil,
            customData: nil
        )
        // Cast the media
        mediaControlChannel?.loadMedia(mediaInformation, autoplay: true)
    }
}
  • ここでは、映像読み込み中に出る画像やテキストの情報をセットしています。 AppleTVにはない親切設計!
        // Define Media Metadata.
        let metadata = GCKMediaMetadata()
        metadata.setString("Big Buck Bunny (2008)", forKey: kGCKMetadataKeyTitle)
        metadata.setString("Cast test.", forKey: kGCKMetadataKeySubtitle)

        let url = NSURL(string:"https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/images/BigBuckBunny.jpg")
        metadata.addImage(GCKImage(URL: url, width: 480, height: 360))
  • 映像の情報はここ。結構いろいろ指定しなくてはダメのようですね。
        // Define Media Information.
        let mediaInformation = GCKMediaInformation(
            contentID:
            "https://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4",
            streamType: GCKMediaStreamType.None,
            contentType: "video/mp4",
            metadata: metadata,
            streamDuration: 0,
            mediaTracks: [],
            textTrackStyle: nil,
            customData: nil
        )
  • 映像データの読み込み開始命令です。
        // Cast the media
        mediaControlChannel?.loadMedia(mediaInformation, autoplay: true)

Castはアプリ終了しても、送った映像は流れたままなんですね〜!
それの設定は特にしなくてもいいようです。便利。


以上で、完了なります。
こちらのサンプルはGithubにあげておきます。

間違っている点などありましたら、ご意見いただけると嬉しいです。

9
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
8