3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

SwiftでYouTubeの特定の動画を再生するアプリを作成した時の気づきについて

Posted at

この記事では、「SwiftでYouTubeの特定の動画を再生するアプリを作成した時の気づきについて」記載します。

<作成環境>
■PC:MacBook Air (11-inch, Mid 2012)
■macOS:10.14.4
■Xcode:10.2.1
■使用言語:Swift 4

<事前準備>
・CocoaPodsのインストール
(参考URL:https://qiita.com/ume1126/items/9ec378c02ca1b06287e9)

<参考URL>
https://dev.classmethod.jp/smartphone/youtube-player-ios-helper/

<気づき>
●「youtube-player-ios-helper」のバージョンについて、GitHubで確認して、Podfileに記述する
こと(2019/10/19現在 Version: 0.1.6)

●再生したいYouTubeの動画ページの中にある「共有」から「埋め込む」選択し、その中にある文字列の中の「src="https://www.youtube.com/embed/-ssjsjsjsjsj」から
最後の/から続く文字列を控えること(例:-ssjsjsjsjsj)
この文字列をコード内の「withVideoId: "-ssjsjsjsjsj"」に記述する

●動画をループで再生したい場合は、
「playerVars: ["playsinline":1, "loop":1]」
と記載すること
(参考URL:https://developers.google.com/youtube/player_parameters?hl=ja)

●停止ボタンを押した際、ラベルのステータスが変化しないので、下記2行をswitch文に記載
case YTPlayerState.unknown:
stateLabel.text = ""
case YTPlayerState.queued:
stateLabel.text = ""

下記がコードになります。

ViewController.swift
import UIKit
import youtube_ios_player_helper

class ViewController: UIViewController, YTPlayerViewDelegate {
    
    @IBOutlet weak var playerView: YTPlayerView!
    
    @IBAction func tapPlay(_ sender: Any) {
        self.playerView.playVideo()
    }
    
    @IBAction func tapPause(_ sender: Any) {
        self.playerView.pauseVideo()
    }
    
    @IBAction func tapStop(_ sender: Any) {
        self.playerView.stopVideo()
    }
    
    @IBOutlet weak var stateLabel: UILabel!
        
    override func viewDidLoad() {
        super.viewDidLoad()
        
        playerView.delegate = self;
        self.playerView.load(withVideoId: "-ssjsjsjsjsj", playerVars: ["playsinline":1, "loop":1])
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    func playerView(_ playerView: YTPlayerView, didChangeTo state: YTPlayerState) {

        switch (state) {
        case YTPlayerState.unstarted:
            stateLabel.text = "停止中"
        case YTPlayerState.playing:
            stateLabel.text = "再生中"
        case YTPlayerState.paused:
            stateLabel.text = "一時停止中"
        case YTPlayerState.buffering:
            stateLabel.text = "バッファリング中"
        case YTPlayerState.ended:
            stateLabel.text = "再生終了"
        case YTPlayerState.unknown:
            stateLabel.text = ""
        case YTPlayerState.queued:
            stateLabel.text = ""
        default:
            break
        }
    }

}
3
4
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
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?