iOS 9中的MPMoviePlayer

时间:2015-09-17 20:49:52

标签: ios swift avplayer ios9 mpmovieplayer

我已经在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
}

3 个答案:

答案 0 :(得分:2)

AVPlayerViewController是UIViewController的子类。因此,不要使用常规视图控制器来创建自定义电影播放器​​控制器,如下所示:

  

不要继承AVPlayerViewController。覆盖这个类   方法不受支持,导致未定义的行为。

试试这样:

var a = moment([2007, 0, 29]);
var b = moment([2007, 0, 28]);
a.diff(b, 'days') //[days, years, months, seconds, ...]
//Result 1 

要将其用作背景,您可以执行以下操作:

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.
    }
}

答案 1 :(得分:1)

适用于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)
}

答案 2 :(得分:0)

导入UIKit 导入MediaPlayer 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
       }
       }