Help us understand the problem. What is going on with this article?

Swiftを使ってMacで動画再生するメモ(AVPlayerView)

More than 5 years have passed since last update.

参考: 006 動画の再生 - Swift Docs

Swiftを使ってMacで動画再生するアプリを作りたいので、とりあえずAVPlayerViewというやつを使ってみた。
ほぼ上のiOS用ドキュメント通りで動いた。

StoryboardからAVPlayerViewを設置してOutletを作って以下の通りでバンドルした動画リソースの再生までできた。お手軽。

import Cocoa
import AVKit
import Foundation
import AVFoundation

class ViewController: NSViewController {

    @IBOutlet weak var playerView: AVPlayerView!

    var videoPlayer:AVPlayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        // パスからassetを生成.
        let path = NSBundle.mainBundle().pathForResource("sample", ofType: "mov")
        let fileURL = NSURL(fileURLWithPath: path!)
        let avAsset = AVURLAsset(URL: fileURL, options: nil)

        // AVPlayerに再生させるアイテムを生成.
        let playerItem = AVPlayerItem(asset: avAsset)

        // AVPlayerを生成.
        videoPlayer = AVPlayer(playerItem: playerItem)

        playerView.player = videoPlayer

        videoPlayer.play()

    }

    override var representedObject: AnyObject? {
        didSet {
        // Update the view, if already loaded.
        }
    }
}

名称未設定 2.png

気づいたこと

  • 上ではvideoPlayer.play()してるけど、別になくても勝手に再生が始まる
    • むしろ自動再生させない方法がわからない
  • 再生・一時停止、シークバー操作、タイムコードなどは特に何もしなくても動作する
  • playerView.showsFullScreenToggleButton = trueを追加すると最大化ボタンが出てきて、実際に動作する
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした