我有一個背景視頻,當我開始我的應用程序播放。我想要視頻暫停並恢復它停止的位置,如果我碰巧碰到了主屏幕按鈕。這裏是我的代碼:從前景導航到背景時,如何暫停播放背景視頻?
class ViewController: UIViewController
{
let moviePlayerController = AVPlayerViewController()
var aPlayer = AVPlayer()
func playBackgroundMovie()
{
if let url = NSBundle.mainBundle().URLForResource("IMG_0489", withExtension: "m4v")
{
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()
{
// Do any additional setup after loading the view, typically from a nib.
super.viewDidLoad()
playBackgroundMovie()
NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(ViewController.didPlayToEndTime), name: AVPlayerItemDidPlayToEndTimeNotification, object: nil)
}
我是否必須在應用程序委託中執行一些操作?我查看了以前的視頻,但我認爲其中一些正在使用過時的swift版本。或者對我來說有點太混亂。
非常感謝!工作! –
甜!很高興工作!你介意接受答案,如果它爲你工作或upvoting它?謝謝 –
對不起,新的這一點。檢查,但我沒有足夠的積分投票不幸的。謝謝,我會投票,一旦我做! –