Swiftではじめる動画再生
-
Upload
yusuke-ariyoshi -
Category
Engineering
-
view
1.043 -
download
0
Transcript of Swiftではじめる動画再生
Swiftではじめる動画再生Yusuke Ariyoshi 2015.5.9
自己紹介• Yusuke Ariyoshi
• フリーランスiOSエンジニア
• Swift暦(半年~1年)
• 受託開発、個人アプリ制作(10作ほど)
アプリで動画を再生する
Demo
iOSで動画を再生する• AVPlayer
• AVPlayerViewController
• MPMoviePlayerController
• MPMoviePlayerViewController
iOSで動画を再生する• AVPlayer
• AVPlayerViewController
• MPMoviePlayerController
• MPMoviePlayerViewController
AVFoundation.framework
AVKit.framework
iOSで動画を再生する• AVPlayer
• AVPlayerViewController
• MPMoviePlayerController
• MPMoviePlayerViewControllerMediaPlayer.framework
iOSで動画を再生する• AVPlayer
• AVPlayerViewController
• MPMoviePlayerController
• MPMoviePlayerViewController
UIと表示、iOS8で追加
UI付き動画再生
modal表示などに利用
動画再生
iOSで動画を再生する• AVPlayer
• AVPlayerViewController
• MPMoviePlayerController
• MPMoviePlayerViewController
今回利用するクラス
Main.Storyboard
D&D
YourViewController.swiftimport AVFoundation import AVKit
class YourViewController: AVPlayerViewController {
override func viewDidLoad() { super.viewDidLoad()
~
}
YourViewController.swift// 動画ファイルのURLを取得 let path = NSBundle.mainBundle().pathForResource("hogevideo", ofType: "mp4")! let url = NSURL(fileURLWithPath: path) // アイテム取得 let playerItem = AVPlayerItem(URL: url) // 生成 let player = AVPlayer(playerItem: playerItem) // 再生 player.play()
UIの外観
カスタマイズするなら
• AVPlayer
• 自作UI
以下を自分で実装 ・Play, Pause等のボタン ・経過時間表示 ・動画時間表示 (or 残時間表示) ・プログレス表示 ・音量表示 ・各タッチハンドリング ・etc
Swiftのオプショナル
YourViewController.swift// 動画ファイルのURLを取得 let path = NSBundle.mainBundle().pathForResource("hogevideo", ofType: "mp4")! let url = NSURL(fileURLWithPath: path)
YourViewController.swift// 動画ファイルのURLを取得 let bundle = NSBundle.mainBundle() let path = bundle.pathForResource("hogevideo", ofType: "mp4")! let url = NSURL(fileURLWithPath: path)
YourViewController.swift// 動画ファイルのURLを取得 let bundle = NSBundle.mainBundle() let path = bundle.pathForResource("hogevideo", ofType: "mp4")! let url = NSURL(fileURLWithPath: path)
YourViewController.swift// 動画ファイルのURLを取得 let bundle = NSBundle.mainBundle() let path = bundle.pathForResource("hogevideo", ofType: "mp4") let url = NSURL(fileURLWithPath: path) // コンパイルエラー
YourViewController.swift// 動画ファイルのURLを取得 let bundle = NSBundle.mainBundle() let path = bundle.pathForResource("hogevideo", ofType: "mp4") let url = NSURL(fileURLWithPath: path!) //これはOK
細かく見てみると…
class NSBundleclass func pathForResource(name: String?, ofType ext: String?, inDirectory bundlePath: String) -> String?
class NSBundleclass func pathForResource(name: String?, ofType ext: String?, inDirectory bundlePath: String) -> String?
class NSURLinit?(fileURLWithPath path: String)
class NSURLinit?(fileURLWithPath path: String)
YourViewController.swift// 動画ファイルのURLを取得 let bundle = NSBundle.mainBundle() let path: String? = bundle.pathForResource("hogevideo", ofType: "mp4") let url = NSURL(fileURLWithPath: path) // Stringを受け取る。String?はコンパイルエラー
YourViewController.swift// 動画ファイルのURLを取得 let bundle = NSBundle.mainBundle() let path: String? = bundle.pathForResource("hogevideo", ofType: "mp4") let url = NSURL(fileURLWithPath: path!) // OK
Sample Code• 全画面表示、埋め込み表示、
リピート再生 (通知→頭出し) を実装しました
• Githubレポジトリ
https://github.com/ariiyu/VideoPlayerSample
Thanks!