本文主要是介绍swiftnbsp;fornbsp;iosnbsp;视频播放,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
swift视频播放一般常用的方式有两种 AVPlayer 和MPMoviePlayer
这些方法直接绑定一个viewController不需要任何控件就可以用 非常方便
代码
方式一(推荐用,功能比较灵活)
import UIKit
import AVFoundation
import AVKit
class T_PlayerViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL(string: "http://www.zghkt.cn/t_tmp.php?tid=23&url=http://file.zghkt.cn/hua_xu_cheng_pian_.mp4")
let player = AVPlayer(URL: url!)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.view.addSubview(playerController.view)
playerController.view.frame = self.view.frame
player.play()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
第二种方法(已过时)
import UIKit
import MediaPlayer
class T_PlayerViewController: UIViewController {
var moviePlayer:MPMoviePlayerController?
override func viewDidLoad() {
super.viewDidLoad()
// //定义一个视频文件路径
// let filePath = NSBundle.mainBundle().pathForResource("sample130", ofType: "mp4")
// //定义一个视频播放器,通过本地文件路径初始化
// moviePlayer = MPMoviePlayerController(contentURL: NSURL(fileURLWithPath: filePath!))
//播放在线视频
moviePlayer = MPMoviePlayerController(contentURL: NSURL(string: "http://www.zghkt.cn/t_tmp.php?tid=23&url=http://file.zghkt.cn/hua_xu_cheng_pian_.mp4"))
//设置播放器样式 - 没有done
moviePlayer!.controlStyle = MPMovieControlStyle.Embedded
//设置大小和位置
moviePlayer?.view.frame = self.view.frame
//添加到界面上
self.view.addSubview(moviePlayer!.view)
//开始播放
moviePlayer?.play()
}
override funcdidReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
这篇关于swiftnbsp;fornbsp;iosnbsp;视频播放的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!