The Media player framework plays local video, audio, and video and audio online.
1, the player Mpmoviecontrolstyle style has the following types:(1)None: No playback control controls(2)Embedded: Embedded playback controls. No done button(3)fullscreen: Full screen playback with controls such as playback progress, done buttons, and fast forward
2, play local video
1234567891011121314151617181920212223242526272829 |
import UIKit
import MediaPlayer
class ViewController
:
UIViewController {
var moviePlayer:
MPMoviePlayerController
?
override func viewDidLoad() {
super
.viewDidLoad()
//定义一个视频文件路径
let filePath =
NSBundle
.mainBundle().pathForResource(
"sample130"
, ofType:
"mp4"
)
//定义一个视频播放器,通过本地文件路径初始化
moviePlayer =
MPMoviePlayerController
(contentURL:
NSURL
(fileURLWithPath: filePath!))
//设置播放器样式 - 全屏
moviePlayer!.controlStyle =
MPMovieControlStyle
.
Fullscreen
//设置大小和位置
moviePlayer?.view.frame =
self
.view.frame
//添加到界面上
self
.view.addSubview(moviePlayer!.view)
//开始播放
moviePlayer?.play()
}
override func didReceiveMemoryWarning() {
super
.didReceiveMemoryWarning()
}
}
|
3, Play online video
1 |
moviePlayer = MPMoviePlayerController (contentURL: NSURL (string: "http://hangge.com/demo.mp4" )) |
Swift-Play local video with media player, online video