iOS 9 中的 MPMoviePlayer
MPMoviePlayer in iOS 9
我已经在 iOS 8 中创建了一个在后台播放电影的功能,现在当我想在 iOS 9 中使用它时,它给了我一个警告并说最好不要使用 MPMoviePlayer 而不是使用 AVPlayer。但我对 AVPlayer 一无所知。我如何在没有警告的情况下将其转换为使用 AVPlayer 而不是 MPMoviePlayer 的正确函数?
这是函数:
func playVideo() ->Bool {
let path = NSBundle.mainBundle().pathForResource("video", ofType:"mov")
//take path of video
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
//asigning video to moviePlayer
if let player = moviePlayer {
player.view.frame = self.view.bounds
//setting the video size to the view size
player.controlStyle = MPMovieControlStyle.None
//Hiding the Player controls
player.prepareToPlay()
//Playing the video
player.repeatMode = .One
//Repeating the video
player.scalingMode = .AspectFill
//setting the aspect ratio of the player
self.view.addSubview(player.view)
self.view.addSubview(blurView)
self.view.sendSubviewToBack(blurView)
self.view.sendSubviewToBack(player.view)
//adding the player view to viewcontroller
return true
}
return false
}
AVPlayerViewController是UIViewController的子类。因此,不要使用常规视图控制器,而是按如下方式创建自定义电影播放器控制器:
Do not subclass AVPlayerViewController. Overriding this class’s
methods is unsupported and results in undefined behavior.
这样试试:
import UIKit
import AVKit
import AVFoundation
class MoviePlayerViewController: AVPlayerViewController {
override func viewDidLoad() {
super.viewDidLoad()
player = AVPlayer(URL: url)
videoGravity = AVLayerVideoGravityResizeAspect
showsPlaybackControls = true
// player?.play() // uncomment this line to autoplay
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didPlayToEndTime", name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}
func didPlayToEndTime(){
print("didPlayToEndTime")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
要将其用作背景,您可以按照以下步骤操作:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
let moviePlayerController = AVPlayerViewController()
var aPlayer = AVPlayer()
func playBackgroundMovie(){
if let url = NSBundle.mainBundle().URLForResource("video", withExtension: "mov") {
aPlayer = AVPlayer(URL: url)
}
moviePlayerController.player = aPlayer
moviePlayerController.view.frame = view.frame
moviePlayerController.view.sizeToFit()
moviePlayerController.videoGravity = AVLayerVideoGravityResizeAspect
moviePlayerController.showsPlaybackControls = false
aPlayer.play()
view.insertSubview(moviePlayerController.view, atIndex: 0)
}
func didPlayToEndTime(){
aPlayer.seekToTime(CMTimeMakeWithSeconds(0, 1))
aPlayer.play()
}
override func viewDidLoad() {
super.viewDidLoad()
playBackgroundMovie()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didPlayToEndTime", name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
工作于 ios 9
import AVFoundation
let path = NSBundle.mainBundle().pathForResource("videoName", ofType:"mp4")
let url = NSURL(fileURLWithPath: path!)
let playerItem = AVPlayerItem(URL: url)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.height)
videoView.layer.addSublayer(playerLayer)
player.actionAtItemEnd = AVPlayerActionAtItemEnd.None
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "playerItemDidReachEnd",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: player.currentItem)
player.play()
func playAction()
{
playerItem.seekToTime(kCMTimeZero)
player.play()
playButton.removeFromSuperview()
playButton = nil
}
func playerItemDidReachEnd()
{
let buttonImage = UIImage(named: "playButton.png")
playButton = UIButton(frame: CGRectMake((self.view.frame.width - 168) / 2, (self.view.frame.height - 110) / 2, 168, 110))
playButton.setImage(buttonImage, forState: .Normal)
playButton.alpha = 0.6
playButton.addTarget(self, action: "playAction", forControlEvents: .TouchUpInside)
self.view.addSubview(playButton)
}
导入 UIKit
导入媒体播放器
class videoView:UIViewController {
var videoPlayer:MPMoviePlayerController!
override func viewDidLoad() {
super.viewDidLoad()
let videoString:String = Bundle.main.path(forResource: "here you can place your video name", ofType:".mp4")!
let videoURL = NSURL.init(fileURLWithPath: videoString)
videoPlayer = MPMoviePlayerController(contentURL: videoURL as URL!)
videoPlayer.view.frame = CGRect(x: 0, y: 0, width: videoView.frame.size.width, height: videoView.frame.size.height)
videoView.addSubview(videoPlayer.view)
videoPlayer.prepareToPlay()
videoPlayer.shouldAutoplay = false
videoPlayer.isFullscreen = false
videoPlayer.controlStyle = MPMovieControlStyle.embedded
}
}
我已经在 iOS 8 中创建了一个在后台播放电影的功能,现在当我想在 iOS 9 中使用它时,它给了我一个警告并说最好不要使用 MPMoviePlayer 而不是使用 AVPlayer。但我对 AVPlayer 一无所知。我如何在没有警告的情况下将其转换为使用 AVPlayer 而不是 MPMoviePlayer 的正确函数? 这是函数:
func playVideo() ->Bool {
let path = NSBundle.mainBundle().pathForResource("video", ofType:"mov")
//take path of video
let url = NSURL.fileURLWithPath(path!)
moviePlayer = MPMoviePlayerController(contentURL: url)
//asigning video to moviePlayer
if let player = moviePlayer {
player.view.frame = self.view.bounds
//setting the video size to the view size
player.controlStyle = MPMovieControlStyle.None
//Hiding the Player controls
player.prepareToPlay()
//Playing the video
player.repeatMode = .One
//Repeating the video
player.scalingMode = .AspectFill
//setting the aspect ratio of the player
self.view.addSubview(player.view)
self.view.addSubview(blurView)
self.view.sendSubviewToBack(blurView)
self.view.sendSubviewToBack(player.view)
//adding the player view to viewcontroller
return true
}
return false
}
AVPlayerViewController是UIViewController的子类。因此,不要使用常规视图控制器,而是按如下方式创建自定义电影播放器控制器:
Do not subclass AVPlayerViewController. Overriding this class’s methods is unsupported and results in undefined behavior.
这样试试:
import UIKit
import AVKit
import AVFoundation
class MoviePlayerViewController: AVPlayerViewController {
override func viewDidLoad() {
super.viewDidLoad()
player = AVPlayer(URL: url)
videoGravity = AVLayerVideoGravityResizeAspect
showsPlaybackControls = true
// player?.play() // uncomment this line to autoplay
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didPlayToEndTime", name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}
func didPlayToEndTime(){
print("didPlayToEndTime")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
要将其用作背景,您可以按照以下步骤操作:
import UIKit
import AVKit
import AVFoundation
class ViewController: UIViewController {
let moviePlayerController = AVPlayerViewController()
var aPlayer = AVPlayer()
func playBackgroundMovie(){
if let url = NSBundle.mainBundle().URLForResource("video", withExtension: "mov") {
aPlayer = AVPlayer(URL: url)
}
moviePlayerController.player = aPlayer
moviePlayerController.view.frame = view.frame
moviePlayerController.view.sizeToFit()
moviePlayerController.videoGravity = AVLayerVideoGravityResizeAspect
moviePlayerController.showsPlaybackControls = false
aPlayer.play()
view.insertSubview(moviePlayerController.view, atIndex: 0)
}
func didPlayToEndTime(){
aPlayer.seekToTime(CMTimeMakeWithSeconds(0, 1))
aPlayer.play()
}
override func viewDidLoad() {
super.viewDidLoad()
playBackgroundMovie()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "didPlayToEndTime", name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
工作于 ios 9
import AVFoundation
let path = NSBundle.mainBundle().pathForResource("videoName", ofType:"mp4")
let url = NSURL(fileURLWithPath: path!)
let playerItem = AVPlayerItem(URL: url)
let player = AVPlayer(playerItem: playerItem)
let playerLayer = AVPlayerLayer(player: player)
playerLayer.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.height)
videoView.layer.addSublayer(playerLayer)
player.actionAtItemEnd = AVPlayerActionAtItemEnd.None
NSNotificationCenter.defaultCenter().addObserver(self,
selector: "playerItemDidReachEnd",
name: AVPlayerItemDidPlayToEndTimeNotification,
object: player.currentItem)
player.play()
func playAction()
{
playerItem.seekToTime(kCMTimeZero)
player.play()
playButton.removeFromSuperview()
playButton = nil
}
func playerItemDidReachEnd()
{
let buttonImage = UIImage(named: "playButton.png")
playButton = UIButton(frame: CGRectMake((self.view.frame.width - 168) / 2, (self.view.frame.height - 110) / 2, 168, 110))
playButton.setImage(buttonImage, forState: .Normal)
playButton.alpha = 0.6
playButton.addTarget(self, action: "playAction", forControlEvents: .TouchUpInside)
self.view.addSubview(playButton)
}
导入 UIKit 导入媒体播放器 class videoView:UIViewController {
var videoPlayer:MPMoviePlayerController!
override func viewDidLoad() {
super.viewDidLoad()
let videoString:String = Bundle.main.path(forResource: "here you can place your video name", ofType:".mp4")!
let videoURL = NSURL.init(fileURLWithPath: videoString)
videoPlayer = MPMoviePlayerController(contentURL: videoURL as URL!)
videoPlayer.view.frame = CGRect(x: 0, y: 0, width: videoView.frame.size.width, height: videoView.frame.size.height)
videoView.addSubview(videoPlayer.view)
videoPlayer.prepareToPlay()
videoPlayer.shouldAutoplay = false
videoPlayer.isFullscreen = false
videoPlayer.controlStyle = MPMovieControlStyle.embedded
}
}