视频一次又一次地播放为背景iOS

时间:2015-11-11 19:45:27

标签: video swift2 ios9

在我的应用程序中,欢迎屏幕将视频作为背景而不是图像,并且将有按钮和子视图。我尝试了这个,但只有一个空白视频显示工具栏暂停和播放。我宁愿让视频循环,没有工具栏或任何东西,只是它自己。最好的方法是什么?

谢谢!

import UIKit
import AVKit
import AVFoundation

class CreateAccountViewController: UIViewController {

var url = "MYURL"
var moviePlayer = AVPlayerViewController()

//label properties and button go here

override func viewDidLoad() {
    super.viewDidLoad()

//        let videoURL = NSURL(string: "MYURL")
//        let player = AVPlayer(URL: videoURL!)
//        let playerLayer = AVPlayerLayer(player: player)
//        playerLayer.frame = self.view.bounds
//        self.view.layer.addSublayer(playerLayer)
//        player.play()

    let URL = NSURL(string: url)
    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()

}

}

1 个答案:

答案 0 :(得分:0)

MPMoviePlayerViewController更改为AVPlayer时,我遇到了同样的问题。

您应该执行以下操作(抱歉,目标 - C代码,暂时不要在Switf上使用):

  1. 注册(viewDidLoad或在任何其他舒适的地方)AVPlayerItemDidPlayToEndTimeNotification

    [[NSNotificationCenter defaultCenter]  addObserver:self selector:@selector(stopVideo:) name:AVPlayerItemDidPlayToEndTimeNotification object:[player currentItem]];
    
  2. 实施stopVideo:,将在上述通知中调用,您可以取消注册以上通知,并将您的播放器设置为nil,因此无法播放:

    -(void)stopVideo:(NSNotification *) notification
    {
        //any other relevant code goes here
        player = nil;
        [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:[player currentItem]];
    }
    
  3. 这对我有用,希望它有所帮助。