watchOS(watchKit)でビデオを表示する方法の調査メモ
AppleWatchでビデオを表示する方法を調べてみたので備忘録を残しておく。
公式
まずは公式サイトの情報を確認する。
冒頭、以下のように書いてあるのでビデオの再生に対応していることがわかる。
Apps can play short video and audio clips while the app is active and running in the foreground. You can embed clips directly in your interface using a movie element and play video inline or using a separate interface.
再生するための条件に色々と制約があるが特定のフォーマットであれば再生できる模様。推奨されているフォーマット、解像度などはこちら。
今回は自作のアプリでビデオを再生したいのでUIフレームワークやAPIを探す必要がある。Apple DeveloperのサイトからwatchKitを調べると、それっぽいクラスが3つ見つかったので順番に調べてみる。
Images and Moviesに関連するclassがある。
class WKInterfaceMovie
An interface element that lets you play video and audio content in your watchOS app.
class WKInterfaceInlineMovie
An interface element that displays a video's poster image and supports inline playing of the video.
class WKInterfaceHMCamera
An interface element that displays either a video stream or a single snapshot from an IP camera connected to HomeKit.
順番に軽くみてみる。
WKInterfaceMovie
A movie object displays a poster image with a play button on top of it. When the user taps the play button, WatchKit plays the movie in a modal interface.
ポスター画像(サムネイルのようなもの?)をクリックすると再生が始まるUIコントロールのようだ。WatchKit plays the movie in a modal interfaceとあるのでモーダルの画面が表示されてその上で再生するタイプだと思われる。
再生するコンテンツは以下のメソッドで指定することができる。URLであればリモートの(サーバ上の)コンテンツにも対応していると思われる。
func setMovieURL(_ URL: URL)
WKInterfaceInlineMovie
This object displayed a poster image for the video, and when the user tapped the poster image, the video was shown in a separate, full-screen, modal view.
こちらは先ほどのclassと違いインラインでの再生にも対応している。先ほどのWKInterfaceMovieとほぼ同じメソッドが用意されているので使い方は一緒のようだが、再生や停止のメソッドが用意されているので、自前でUIを用意して制御することを想定したコンポーネントであることがわかる。細かいことをする場合はこちらを使う方が良さそうな印象。
WKInterfaceHMCamera
An interface element that displays either a video stream or a single snapshot from an IP camera connected to HomeKit.
HomeKitに対応したIPカメラの映像を表示するコンポーネントのようだ。具体的な使い方はよく分からない。ネットを検索しても殆ど情報が見つからないので深追いするのは難しそう。
まとめ
watchOSでビデオを再生する方法を調べた結果、WKInterfaceMovieとWKInterfaceInlineMovieが利用できそうだと分かった。次は実際にこれらのclassを使ってビデオを再生してみようと思う。