How to embed and stream a video from the web within an Xcode macOS Cocoa Application?

爱⌒轻易说出口 提交于 2020-01-07 04:42:27

问题


I am trying to get my macOS Cocoa Application Xcode project to play some video when I feed it a specified URL.

To simplify the question lets just use an empty Xcode project.

I want to achieve the same level of control as I am able to get using AVPlayerViewController() in my iOS Single View Application. Within that app I am currently using the solution offered here.

However, only the second example works in my macOS Cocoa Application when adapted like so:

import Cocoa
import AVFoundation
import AVKit

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
        let player = AVPlayer(url: videoURL!)
        let playerLayer = AVPlayerLayer(player: player)
        playerLayer.frame = self.view.bounds
        self.view.layer?.addSublayer(playerLayer)
        player.play()

    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

This option however has 2 downsides:

  1. It creates a repetitive error message in the console:

    2017-06-27 18:22:06.833441+0200 testVideoOnmacOS[24456:1216773] initWithSessionInfo: XPC connection interrupted
    2017-06-27 18:22:06.834830+0200 testVideoOnmacOS[24456:1216773] startConfigurationWithCompletionHandler: Failed to get remote object proxy: Error Domain=NSCocoaErrorDomain Code=4097 "connection to service named com.apple.rtcreportingd" UserInfo={NSDebugDescription=connection to service named com.apple.rtcreportingd}
    
  2. This AVPlayer solution does not offer the same control as the AVPlayerViewController() solution.

So I attempted to get the following AVPlayerViewController() solution to work. But it fails:

import Cocoa
import AVFoundation
import AVKit

class ViewController: NSViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
        let player = AVPlayer(url: videoURL!)
        let playerViewController = AVPlayerViewController()
        playerViewController.player = player
        self.present(playerViewController, animated: true) {
            playerViewController.player!.play()
        }

    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


}

The error I am getting is:

Use of unresolved identifier 'AVPlayerViewController' - Did you mean 'AVPlayerViewControlsStyle'?

I have tried to work around this in many different ways. It will be confusing if I spell them all out. My prominent problem seems to be that the vast majority of online examples are iOS based.

Thus, the main question:

How would I be able to successfully use AVPlayerViewController within my Xcode macOS Cocoa Application?


回答1:


Yes, got it working!

Thnx to ninjaproger's comment.

This seems to work in order to get a video from the web playing / streaming on a macOS Cocoa Application in Xcode.

First, create a AVKitPlayerView on your storyboard and link it as an IBOutlet to your NSViewController.

Then, simply apply this code:

import Cocoa
import AVFoundation
import AVKit

class ViewController: NSViewController {
    @IBOutlet var videoView: AVPlayerView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let videoURL = URL(string: "https://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
        let player = AVPlayer(url: videoURL!)
        let playerViewController = videoView
        playerViewController?.player = player
        playerViewController?.player!.play()

    }

    override var representedObject: Any? {
        didSet {
        // Update the view, if already loaded.
        }
    }


} 


来源:https://stackoverflow.com/questions/44785787/how-to-embed-and-stream-a-video-from-the-web-within-an-xcode-macos-cocoa-applica

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!