準備
MediaPlayer.framework を追加する。
MediaPlayer をインポート
import MediaPlayer
実装例
ウェブ上の動画を全画面で再生させる例。
ViewController.swift
import UIKit
import MediaPlayer
class ViewController: UIViewController {
var moviePlayer:MPMoviePlayerController!
override func viewDidLoad() {
super.viewDidLoad()
var url:NSURL = NSURL(string: "http://sample.com/video.mov")!
self.moviePlayer = MPMoviePlayerController(contentURL: url)
self.moviePlayer.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: self.view.frame.height)
self.view.addSubview(moviePlayer.view)
self.moviePlayer.fullscreen = true
self.moviePlayer.controlStyle = MPMovieControlStyle.Embedded
self.moviePlayer.repeatMode = MPMovieRepeatMode.One
NSNotificationCenter.defaultCenter().addObserver(self, selector: "onMPMoviePlayerPlaybackDidFinishNotificationReceived:", name: MPMoviePlayerPlaybackDidFinishNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "onMPMoviePlayerLoadStateDidChangeNotificationReceived:", name: MPMoviePlayerLoadStateDidChangeNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "onMPMoviePlayerWillEnterFullscreenNotificationReceived:", name: MPMoviePlayerWillEnterFullscreenNotification, object: nil)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "onMPMoviePlayerWillExitFullscreenNotificationReceived:", name: MPMoviePlayerWillExitFullscreenNotification, object: nil)
}
func onMPMoviePlayerPlaybackDidFinishNotificationReceived(notification: NSNotification){
let userInfo: NSDictionary = notification.userInfo!
let reason = userInfo[MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] as Int
println("onMPMoviePlayerPlaybackDidFinishNotificationReceived = " + String(reason))
}
func onMPMoviePlayerLoadStateDidChangeNotificationReceived(notification: NSNotification){
let state = self.moviePlayer.loadState
println("onMPMoviePlayerLoadStateDidChangeNotificationReceived = " + String(state.rawValue))
}
func onMPMoviePlayerWillEnterFullscreenNotificationReceived(notification: NSNotification){
println("onMPMoviePlayerWillEnterFullscreenNotificationReceived")
}
func onMPMoviePlayerWillExitFullscreenNotificationReceived(notification: NSNotification){
println("onMPMoviePlayerWillExitFullscreenNotificationReceived")
}
}
プロパティ
MPMoviePlayerController では振る舞いを細かく指定できます。
controlStyle
再生コントロールをどのように表示するか。選択できる値は下記の通り。
enum MPMovieControlStyle : Int {
case None // No controls、表示しない
case Embedded // Controls for an embedded view、動画内に埋め込み
case Fullscreen // Controls for fullscreen playback、全画面再生用に表示
}
repeatMode
繰り返し再生するかどうか。選択できる値は下記の通り。
enum MPMovieRepeatMode : Int {
case None // 繰り返しなし
case One // 繰り返す
}
shouldAutoplay
自動で再生開始するか。BOOL 値で指定。
scalingMode
映像をどのようにスケールさせるか。選択できる値は下記の通り。
enum MPMovieScalingMode : Int {
case None // No scaling
case AspectFit // Uniform scale until one dimension fits
case AspectFill // Uniform scale until the movie fills the visible bounds. One dimension may have clipped contents
case Fill // Non-uniform scale. Both render dimensions will exactly match the visible bounds
}
Notification
Player の状態が変わった時は Notification で通知されます。通知してもらういたい要素をいくつか列挙します。
- MPMoviePlayerPlaybackDidFinishNotification
- 再生が終わった時
- この通知はフルスクリーンモード中やユーザーが Done ボタンをタップした時には発火しない。 代わりにMPMoviePlayerDidExitFullscreenNotification を監視すること。
- MPMoviePlayerWillEnterFullscreenNotification
- フルスクリーン再生になる前に通知
- MPMoviePlayerDidEnterFullscreenNotification
- フルスクリーン再生になった後に通知
- MPMoviePlayerWillExitFullscreenNotification
- フルスクリーン再生を終了する前に通知
- MPMoviePlayerDidExitFullscreenNotification
- フルスクリーン再生を終了した後に通知
サポートしているフォーマット
ビデオ
- H.264 video
- up to 1.5 Mbps
- 640 by 480 pixels, 30 frames per second
- Low-Complexity version of the H.264 Baseline Profile with AAC-LC audio up to 160 Kbps, 48 kHz, stereo audio in .m4v, .mp4, and .mov file formats
- H.264 video
- up to 768 Kbps, 320 by 240 pixels
- 30 frames per second
- Baseline Profile up to Level 1.3 with AAC-LC audio up to 160 Kbps, 48 kHz, stereo audio in .m4v, .mp4, and .mov file formats
- MPEG-4 video
- up to 2.5 Mbps
- 640 by 480 pixels, 30 frames per second
- Simple Profile with AAC-LC audio up to 160 Kbps, 48 kHz, stereo audio in .m4v, .mp4, and .mov file formats
オーディオ
- AAC
- Apple Lossless (ALAC)
- A-law
- IMA/ADPCM (IMA4)
- Linear PCM
- µ-law
- DVI/Intel IMA ADPCM
- Microsoft GSM 6.10
- AES3-2003