はじめに
MacOS(Objective-C)で動画どうやって再生するのかなとふと思って、調査しても情報があまりなかったのでこちらでまとめていきます。
いまさらObjective-Cを使うのは、Unityの動画プラグイン作りたいと思ったからです。
Objective-CとMacのデスクトップアプリの知識がほとんどない状態で書いているのでご了承ください。
公式ドキュメント
iOSのサンプルプロジェクトはあったのですが、MacOSのサンプルが無かったのでこちらを参考にシンプルにまとめていきます。
手順
XCode Version 12.4
プロジェクト作成
Framework追加
適当な動画(mov)ファイルをプロジェクトに追加
iOSのサンプルプロジェクトに入っていたアザラシの動画をプロジェクトに追加。
storyboardにAVPlayerViewを追加
ViewController.hにAVPlayerViewの参照を追加
画面分割でヘッダファイルとstoryboardが両方表示されるようにする
一度ViewController.hを以下の状態に
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVPlayerView.h>
@interface ViewController : NSViewController
@property (weak) IBOutlet AVPlayerView *playerView;
@end
ViewController.mを編集
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSURL *movieURL = [[NSBundle mainBundle] URLForResource:@"ElephantSeals" withExtension:@"mov"];
AVURLAsset* avAsset = [AVURLAsset assetWithURL:movieURL];
AVPlayerItem* playerItem = [AVPlayerItem playerItemWithAsset:avAsset];
self.playerView.player = [[AVPlayer alloc] initWithPlayerItem:playerItem];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
// Update the view, if already loaded.
}
@end
実行結果
最後に
結構シンプルに動画を再生することができました。
UIが絡んでくると、手順書くのが大変ですね・・・
Unity上で動画を再生することが最終的な目標なので、まだ戦いは続きます・・・