在按暂停⏯️时禁用Avplayerview停止视频。钥匙

发布于 2025-02-07 07:10:32 字数 1709 浏览 0 评论 0原文

目前,我正在使用Cocoa应用中的Avplayer使用Avplayerview。 该视频播放不错,但我想禁用Avplayerview或Avplayer响应例如使用暂停键⏯️暂停视频的钥匙按钮。 我有一个属性吗?

这是我的代码:

import Cocoa
import AVKit
import AVFoundation

class MainPlayerController: NSViewController {
    private var player: AVPlayer
    private var video_player_view: AVPlayerView
    
    init(video_url: URL ) {
        self.player = AVPlayer(url: video_url)
        self.video_player_view = AVPlayerView.init()
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("NSCoder is not supported")
    }
    
    override func loadView() {
        view = NSView.init()
        view.layer = CALayer.init()
        view.wantsLayer = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        
        video_player_view.frame = view.frame
        
        player.isMuted = true
        
        video_player_view.videoGravity = .resizeAspectFill
        video_player_view.controlsStyle = .none
        video_player_view.player = player
        video_player_view.player?.play()
        view.addSubview(video_player_view)
        
        // Looping video
        player.actionAtItemEnd = AVPlayer.ActionAtItemEnd.none
        NotificationCenter.default.addObserver(self, selector: #selector(player_reached_end(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
        
        player.play()
    }
    
    @objc func player_reached_end(notification: Notification) {
        player.seek(to: CMTime.zero)
    }
 
}

Currently I am using an AVPlayerView in combination with an AVPlayer in my Cocoa App.
The video plays fine but I want to disable that the AVPlayerView or AVPlayer responds to key presses that for example pause the video with the pause key⏯️.
Is there an attribute I am overlooking?

Here is my code:

import Cocoa
import AVKit
import AVFoundation

class MainPlayerController: NSViewController {
    private var player: AVPlayer
    private var video_player_view: AVPlayerView
    
    init(video_url: URL ) {
        self.player = AVPlayer(url: video_url)
        self.video_player_view = AVPlayerView.init()
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("NSCoder is not supported")
    }
    
    override func loadView() {
        view = NSView.init()
        view.layer = CALayer.init()
        view.wantsLayer = true
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        
        video_player_view.frame = view.frame
        
        player.isMuted = true
        
        video_player_view.videoGravity = .resizeAspectFill
        video_player_view.controlsStyle = .none
        video_player_view.player = player
        video_player_view.player?.play()
        view.addSubview(video_player_view)
        
        // Looping video
        player.actionAtItemEnd = AVPlayer.ActionAtItemEnd.none
        NotificationCenter.default.addObserver(self, selector: #selector(player_reached_end(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: player.currentItem)
        
        player.play()
    }
    
    @objc func player_reached_end(notification: Notification) {
        player.seek(to: CMTime.zero)
    }
 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文