LoginSignup
0
0

More than 1 year has passed since last update.

MacOS(Objective-C)で動画を再生する

Posted at

はじめに

MacOS(Objective-C)で動画どうやって再生するのかなとふと思って、調査しても情報があまりなかったのでこちらでまとめていきます。
いまさらObjective-Cを使うのは、Unityの動画プラグイン作りたいと思ったからです。
Objective-CとMacのデスクトップアプリの知識がほとんどない状態で書いているのでご了承ください。

公式ドキュメント

AVFoundation公式ドキュメント

iOSのサンプルプロジェクトはあったのですが、MacOSのサンプルが無かったのでこちらを参考にシンプルにまとめていきます。

手順

XCode Version 12.4

プロジェクト作成

macOSのAppを選択
Screen Shot 2022-08-03 at 19.03.04.png

言語はObjective-Cで
Screen Shot 2022-08-03 at 19.03.44.png

Framework追加

AVFoundationとAVKitを追加
Screen Shot 2022-08-03 at 19.05.33.png

適当な動画(mov)ファイルをプロジェクトに追加

iOSのサンプルプロジェクトに入っていたアザラシの動画をプロジェクトに追加。

68747470733a2f2f71696974612d696d6167652d73746f72652e73332e61702d6e6f727468656173742d312e616d617a6f6e6177732e636f6d2f302f3138383437362f61313161373533332d343631622d363430652d616162362d3237303139363363633633652e706e67.png

Copy items if neededにチェックを入れる
Screen Shot 2022-08-03 at 19.10.25.png

storyboardにAVPlayerViewを追加

Screen Shot 2022-08-03 at 19.14.09.png

Screen Shot 2022-08-03 at 19.36.49.png

ViewController.hにAVPlayerViewの参照を追加

画面分割でヘッダファイルとstoryboardが両方表示されるようにする
Screen Shot 2022-08-03 at 19.20.54.png

一度ViewController.hを以下の状態に

#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVPlayerView.h>

@interface ViewController : NSViewController

@property (weak) IBOutlet AVPlayerView *playerView;

@end

Controlキーを押しながらドラッグ
Screen Shot 2022-08-03 at 19.27.51.png

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

実行結果

Screen Shot 2022-08-03 at 19.38.50.png

最後に

結構シンプルに動画を再生することができました。
UIが絡んでくると、手順書くのが大変ですね・・・
Unity上で動画を再生することが最終的な目標なので、まだ戦いは続きます・・・

0
0
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
0
0