AVPlayer自动播放但不应该(Swift)

时间:2016-10-15 20:51:24

标签: swift avfoundation avplayer avplayerviewcontroller avkit

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

perf %>%
    spread(Task, Performance, fill = 0)
#> # A tibble: 5 x 6
#>   person_id Task1 Task2 Task3 Task4 Task5
#> *     <int> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1         1     1     1     1     1     1
#> 2         2     2     2     2     2     2
#> 3         3     3     3     3     3     3
#> 4         4     4     4     4     4     4
#> 5         5     5     5     5     5     5

它基本上可以正常工作,但玩家会在发布时自动播放,但不应该。即使我在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(),也会自动播放。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您至少应该将AVPlayer放在ViewWillAppear()功能中,暂停playerView.player会暂停整个视图 - 而是暂停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()
}