2016-10-18 23 views
2

ボタンを押したときにムービーを再生するための簡単なiPadアプリを作っています。ムービーが再生され、ムービーが終了するとAVPlayerViewを閉じてメイン画面に戻ります。 現在、ビデオが終了すると、最後のフレームにとどまります。 現在、私のViewController.Swift。ムービーが完成したらAVPlayerを閉じる

import UIKit 
import AVKit 
import AVFoundation 

class ViewController: UIViewController { 
//MARK : Properties 

override func viewDidLoad() { 
    super.viewDidLoad() 
    // Do any additional setup after loading the view, typically from a nib. 
} 
//MARK: Actions 

@IBAction func playButton(_ sender: AnyObject) { 

    let movieURL = Bundle.main.url(forResource: "ElephantSeals", withExtension: "mov")! 
    let player = AVPlayer(url: movieURL as URL) 

    let playerViewController = AVPlayerViewController() 

    playerViewController.player = player 

    self.present(playerViewController, animated: true) { 
     playerViewController.player!.play() 
     } 
// player.actionAtItemEnd = playerViewController.dismiss(animated: true) 
} 
} 

あなたが見ることができるように、私はactionAtItemEndで何かがあるかもしれないと思うが、私はそれを実装するかどうかはわかりません。 ありがとうございます。

答えて

5

このSWIFT 3.0で動作するコードは、このメソッドが呼び出されていない:) ...

import UIKit 
import AVKit 
import AVFoundation 

class ViewController: UIViewController { 

    let playerViewController = AVPlayerViewController() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
    } 

    @IBAction func playButton(_ sender: AnyObject) { 

     let movieURL = Bundle.main.url(forResource: "ElephantSeals", withExtension: "mov")! 
     let player = AVPlayer(url: movieURL as URL) 

     playerViewController.player = player 
     NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: playerViewController.player?.currentItem) 

     self.present(playerViewController, animated: true) { 
      self.playerViewController.player!.play() 
     } 
    } 


    func playerDidFinishPlaying(note: NSNotification) { 
      self.playerViewController.dismiss(animated: true) 
      } 
} 
+0

ご理解いただきありがとうございます。これは私のために働いた! – RoryM

+0

プレーヤーの再生が終了すると、現在の時刻はゼロに設定されません。ユーザーがビデオを再び再生しようとすると、最後に再生が開始されます。 .AVPlayerItemDidPlayToEndTime通知は、「この通知は、オブザーバが別のスレッドにポストされている可能性があります。 AVPlayerViewControllerを無効にするには、メインスレッドで行う必要があります。 – Leon

0

AVPlayerViewControllerDelegateを呼び出します。 viewDidLoadで

は、デリゲートを初期化し、この方法、あなたがこれを行うことができNSNotificationCenterを使用

func playerViewControllerDidStopPictureInPicture(AVPlayerViewController) { 
     AVPlayerViewController.dismiss(animated: true) 
} 

https://developer.apple.com/reference/avkit/avplayerviewcontrollerdelegate

+0

これを試してみて、私に知らせたときに、映像が終了通常のやり方でプレーする。 – Leon

1

を実装します。

NSNotificationCenter.defaultCenter().addObserver(self, selector: "playerDidFinishPlaying:", name: AVPlayerItemDidPlayToEndTimeNotification, object: item) 

スウィフト3.0

NotificationCenter.default.addObserver(self, selector: #selector(playerDidFinishPlaying(note:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil) 


func playerDidFinishPlaying(note: NSNotification) { 
    // here you can do your dismiss controller logic 
    AVPlayerViewController.dismiss(animated: true) 
    print("Video Finished") 
} 
+0

Xcodeはこのコードの最初の行に2つのことを教えてくれました。 「NSNotificationCenter」の名前が「NotificationCenter」 – RoryM

+0

..に変更されました。未解決の識別子 'item' – RoryM

+0

を使用してください。 – KKRocks

関連する問題