Swift - 使用Media Player播放本地视频,在线视频
Media Player框架用于播放本地视频、音频,也可以在线播放视频和音频。
![](http://www.hangge.com/blog_uploads/201506/2015062414263557219.png)
1,播放器MPMovieControlStyle样式有如下几种:
(1)None: 没有播放控制控件
(2)Embedded:嵌入式播放控件。没有Done按钮
(3)FullScreen:全屏播放,有播放进度、Done按钮和快进等控件
![](http://www.hangge.com/blog_uploads/201506/2015062414261215462.png)
![](http://www.hangge.com/blog_uploads/201506/2015062414262429046.png)
![](http://www.hangge.com/blog_uploads/201506/2015062414263557219.png)
2,播放本地视频
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
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,播放在线视频
1
|
|