入門

iOSで動画を再生する

動画を1つ再生する

PlayerViewController.swift
import UIKit
import AVFoundation

class PlayerViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 動画ファイルのURLを取得
        let url = URL(string: "url")

        // 生成
        let player = AVPlayer(url: url!)

        // レイヤーの追加
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer)

        // 再生
        player.play()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

複数の動画を連続で再生する

PlayerViewController.swift
import UIKit
import AVFoundation

class PlayerViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // 動画ファイルのURLを取得
        let url1 = URL(string: "url")
        let url2 = URL(string: "url")
        let url3 = URL(string: "url")
        let url4 = URL(string: "url")
        let url5 = URL(string: "url")

        // アイテム取得
        let playerItem1 = AVPlayerItem(url: url1!)
        let playerItem2 = AVPlayerItem(url: url2!)
        let playerItem3 = AVPlayerItem(url: url3!)
        let playerItem4 = AVPlayerItem(url: url4!)
        let playerItem5 = AVPlayerItem(url: url5!)

        // 生成
        let queuePlayer = AVQueuePlayer(items: [playerItem1, playerItem2, playerItem3, playerItem4, playerItem5])

        // レイヤーの追加
        let playerLayer = AVPlayerLayer(player: queuePlayer)
        playerLayer.frame = self.view.bounds
        self.view.layer.addSublayer(playerLayer)

        // 再生
        queuePlayer.play()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

イベントを取得する(タッチで次の動画へ)

PlayerViewController.swift
import UIKit
import AVFoundation

class PlayerViewController: UIViewController {

    // AVPlayer.
    var queuePlayer : AVQueuePlayer!

    override func viewDidLoad() {
        super.viewDidLoad()

        // メインビューのスタイル追加
        self.view.backgroundColor = UIColor.black

        // 動画ファイルのURLを取得
        let url1 = URL(string: "url")
        let url2 = URL(string: "url")
        let url3 = URL(string: "url")
        let url4 = URL(string: "url")
        let url5 = URL(string: "url")

        // アイテム取得
        let playerItem1 = AVPlayerItem(url: url1!)
        let playerItem2 = AVPlayerItem(url: url2!)
        let playerItem3 = AVPlayerItem(url: url3!)
        let playerItem4 = AVPlayerItem(url: url4!)
        let playerItem5 = AVPlayerItem(url: url5!)

        // 生成
        self.queuePlayer = AVQueuePlayer(items: [playerItem1, playerItem2, playerItem3, playerItem4, playerItem5])

        // レイヤーの追加
        let playerLayer = AVPlayerLayer(player: self.queuePlayer)

        // スタイルの追加
        playerLayer.frame = self.view.bounds
        playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

        // タップして閉じるをやっていく
        let screenView = UIView()
        screenView.frame = self.view.bounds

        // ジェスチャーを追加
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.tapToNext))
        screenView.addGestureRecognizer(tapGestureRecognizer)

        // サブビューの追加
        self.view.layer.addSublayer(playerLayer)
        self.view.addSubview(screenView)

        // 再生
        self.queuePlayer.play()
    }

    // 画面をタップ
    @objc func tapToNext(_ sender: UITapGestureRecognizer) {
        // 次の動画へ
        self.queuePlayer.advanceToNextItem()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

AVQueuePlayerを使わずにAVPlayerで複数ビデオの連続再生

PlayerViewController.swift
import UIKit
import AVFoundation

class PlayerViewController: UIViewController {

    // AVPlayer.
    var player : AVPlayer! = AVPlayer()
    var playerItems: [AVPlayerItem] = []
    var currentTrack = 0

    override func viewDidLoad() {
        super.viewDidLoad()

        // メインビューのスタイル追加
        self.view.backgroundColor = UIColor.black

        // 動画ファイルのURLを取得
        let url1 = URL(string: "url")
        let url2 = URL(string: "url")
        let url3 = URL(string: "url")
        let url4 = URL(string: "url")
        let url5 = URL(string: "url")

        // アイテム取得
        let playerItem1 = AVPlayerItem(url: url1!)
        let playerItem2 = AVPlayerItem(url: url2!)
        let playerItem3 = AVPlayerItem(url: url3!)
        let playerItem4 = AVPlayerItem(url: url4!)
        let playerItem5 = AVPlayerItem(url: url5!)

        // 生成
        playerItems = [playerItem1, playerItem2, playerItem3, playerItem4, playerItem5]

        // レイヤーの追加
        let playerLayer = AVPlayerLayer(player: player)

        // スタイルの追加
        playerLayer.frame = self.view.bounds
        playerLayer.videoGravity = AVLayerVideoGravity.resizeAspectFill

        // タップして閉じるをやっていく
        let screenView = UIView()
        screenView.frame = self.view.bounds

        // ジェスチャーを追加
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.rightAreaTap))
        screenView.addGestureRecognizer(tapGestureRecognizer)

        // サブビューの追加
        self.view.layer.addSublayer(playerLayer)
        self.view.addSubview(screenView)

        // 再生
        self.playTrack()
    }

    func playTrack()
    {
        if self.playerItems.count > 0 {
            self.player.replaceCurrentItem(with: self.playerItems[currentTrack])
            self.player.play()
        }
    }

    // 画面の左側をタップ
    @objc func leftAreaTap(_ sender: UITapGestureRecognizer) {
        // 前の動画へ
        if self.currentTrack - 1 < 0 {
            self.currentTrack = (self.playerItems.count - 1) < 0 ? 0 : (self.playerItems.count - 1)
        } else {
            self.currentTrack -= 1
        }
        self.playTrack()
    }

    // 画面の右側をタップ
    @objc func rightAreaTap(_ sender: UITapGestureRecognizer) {
        // 次の動画へ
        if self.currentTrack + 1 < self.playerItems.count {
            self.currentTrack += 1;
        } else {
            self.currentTrack = 0
        }
        self.playTrack()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}