如何在Xcode macOS Cocoa Application中嵌入和流式传输来自网络的视频? [英] How to embed and stream a video from the web within an Xcode macOS Cocoa Application?
问题描述
我正在尝试让我的 macOS Cocoa应用程序 Xcode项目在输入指定的URL时播放某些视频。
I am trying to get my macOS Cocoa Application Xcode project to play some video when I feed it a specified URL.
为简化问题,我们只使用一个空的Xcode项目。
To simplify the question lets just use an empty Xcode project.
我希望获得与我在<< c> c中使用 AVPlayerViewController()
所能达到的控制水平相同的控制水平。 strong> iOS单一视图应用程序。在该应用程序中,我目前正在此处使用提供的解决方案。
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.
但是,这样修改后,只有第二个示例在我的macOS Cocoa应用程序中有效:
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.
}
}
}
此选项具有 2个缺点:
-
此选项会在控制台中创建重复的错误消息:
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}
此 AVPlayer
解决方案不能提供与 AVPlayerViewController()
解决方案相同的控件。
This AVPlayer
solution does not offer the same control as the AVPlayerViewController()
solution.
所以我试图使以下AVPlayerViewController()解决方案正常工作。但是失败了:
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.
}
}
}
我遇到的错误是:
使用未解析的标识符'AVPlayerViewController'-您是说
'AVPlayerViewControlsStyle'吗?
Use of unresolved identifier 'AVPlayerViewController' - Did you mean 'AVPlayerViewControlsStyle'?
我已经尝试了多种解决方法。如果我把它们全部拼出来,那会让人困惑。我的主要问题似乎是绝大多数在线示例都基于iOS。
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:
我将如何能否在我的Xcode macOS可可应用程序中成功使用AVPlayerViewController?
推荐答案
是,成功了!
感谢ninjaproger的评论。
Thnx to ninjaproger's comment.
此操作似乎是为了使Web上的视频可以在 Xcode Cocoa Application 上播放/流式传输强>。
This seems to work in order to get a video from the web playing / streaming on a macOS Cocoa Application in Xcode.
首先,在情节提要中创建一个 AVKitPlayerView
并将其作为IBOutlet链接到您的NSViewController。
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.
}
}
}
这篇关于如何在Xcode macOS Cocoa Application中嵌入和流式传输来自网络的视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!