1

我在 macOS 应用程序中创建了一个非常简单的 AVPlayer。

import Cocoa
import AVKit
import AVFoundation

class ViewController: NSViewController {

@IBOutlet var playerView: AVPlayerView!

override func viewDidLoad() {

    super.viewDidLoad()

    let videoPath = URL(fileURLWithPath: "myPath/myFile.mp4")
    let videoPlayer = AVPlayer(url: videoPath)
    playerView.player = videoPlayer

    }

}

它基本上可以正常工作,但是播放器在启动时会自动播放,这是不应该的。即使我playerView.player?.pause()在末尾添加viewDidLoad,它也会自动播放。

我究竟做错了什么?

4

1 回答 1

1

您至少应该将您AVPlayerViewWillAppear()函数放入函数中,而暂停playerView.player会暂停您的整个视图 - 而不是 pause videoPlayer

videoPlayer.pause()

添加之后playerView.player = videoPlayer应该可以工作;您的代码将是:

override func viewWillAppear() {
    super.viewWillAppear()

    let videoPath = URL(fileURLWithPath: "myPath/myFile.mp4")
    let videoPlayer = AVPlayer(url: videoPath)
    playerView.player = videoPlayer
    videoPlayer.pause()
}
于 2016-10-15T21:46:18.177 回答