私は素早く新しいです。私は、アプリケーションが起動されると、ウィンドウの背景としてAVPlayerを使用して、アプリケーションのリソースからビデオをループするMac OSアプリケーションを作成しようとしています。ユーザがメニュー項目を選択/ボタンをクリックすると、背景ビデオはアプリケーションのリソースとは異なるビデオに即時に変更され、そのビデオをウィンドウの背景としてループし始めます。Swift Mac OS - ボタンをクリックすると、別の動画を再生する方法/ AVPlayerで動画のURLを変更する方法は?
私はこのチュートリアルの次のアプリが起動したら、最初のビデオを再生することができた:(https://youtu.be/QgeQc587w70)と私はまた、正常にこのポストを以下のシームレスビデオループ自体を作った:(Looping AVPlayer seamlessly)。
私が今直面している問題は、メニュー項目が選択された/ボタンがクリックされたときに、ビデオをもう一方のビデオに変更することです。私が進めていたアプローチは、URLを変更して新しいURLを使用して新しいAVPlayerを作成し、この投稿に続いてplayerView.playerに影響を与えることです(Swift How to update url of video in AVPlayer when clicking on a button?)。しかし、メニュー項目が選択されるたびに、エラー "thread 1 exc_bad_instruction(code = exc_i386_invop subcode = 0x0)"でアプリケーションがクラッシュします。これは明らかにplayerViewの値がnilであることが原因です。私は本当にこれの理由を理解していないplayerViewはxibファイルを使用して作成し、コントロールのドラッグでスワイプファイルにリンクされたAVPlayerViewオブジェクトであり、私が望むことを実行するための別の適切なメソッドを見つけることができなかったする。あなたがこれの理由とそれを修正する方法を知っていれば、私に助けを提供してください。また、私が先に言及したより良い方法を知っていれば教えてください。どんな助けでも大歓迎です!ビデオを早送り/一時停止することはできません
import Cocoa
import AppKit
import AVKit
import AVFoundation
struct videoVariables {
static var videoName = "Test_Video" //declaring the video name as a global variable
}
var videoIsPlaying = true
var theURL = Bundle.main.url(forResource:videoVariables.videoName, withExtension: "mp4") //creating the video url
var player = AVPlayer.init(url: theURL!)
class BackgroundWindow: NSWindowController {
@IBOutlet weak var playerView: AVPlayerView! // AVPlayerView Linked using control-drag from xib file
@IBOutlet var mainWindow: NSWindow!
@IBOutlet weak var TempBG: NSImageView!
override var windowNibName : String! {
return "BackgroundWindow"
}
//function used for resizing the temporary background image and the playerView to the window’s size
func resizeBG() {
var scrn: NSScreen = NSScreen.main()!
var rect: NSRect = scrn.frame
var height = rect.size.height
var width = rect.size.width
TempBG.setFrameSize(NSSize(width: Int(width), height: Int(height)))
TempBG.frame.origin = CGPoint(x: 0, y: 0)
playerView!.setFrameSize(NSSize(width: Int(width), height: Int(height)))
playerView!.frame.origin = CGPoint(x: 0, y: 0)
}
override func windowDidLoad() {
super.windowDidLoad()
self.window?.titleVisibility = NSWindowTitleVisibility.hidden //hide window’s title
self.window?.styleMask = NSBorderlessWindowMask //hide window’s border
self.window?.hasShadow = false //hide window’s shadow
self.window?.level = Int(CGWindowLevelForKey(CGWindowLevelKey.desktopWindow)) //set window’s layer as desktopWindow layer
self.window?.center()
self.window?.makeKeyAndOrderFront(nil)
NSApp.activate(ignoringOtherApps: true)
if let screen = NSScreen.main() {
self.window?.setFrame(screen.visibleFrame, display: true, animate: false) //resizing the window to cover the whole screen
}
resizeBG() //resizing the temporary background image and the playerView to the window’s size
startVideo() //start playing and loop the first video as the window’s background
}
//function used for starting the video again once it has been played fully
func playerItemDidReachEnd(notification: NSNotification) {
playerView.player?.seek(to: kCMTimeZero)
playerView.player?.play()
}
//function used for starting and looping the video
func startVideo() {
//set the seeking time to be 2ms ahead to prevent a black screen every time the video loops
let playAhead = CMTimeMake(2, 100);
//loops the video
NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object:
playerView.player?.currentItem, queue: nil, using: { (_) in
DispatchQueue.main.async {
self.playerView.player?.seek(to: playAhead)
self.playerView.player?.play()
}
})
var playerLayer: AVPlayerLayer?
playerLayer = AVPlayerLayer(player: player)
playerView?.player = player
print(playerView?.player)
playerLayer?.videoGravity = AVLayerVideoGravityResizeAspectFill
player.play()
}
//changing the url to the new url and create a new AVPlayer then affect it to the playerView.player once the menu item is being selected
@IBAction func renderBG(_ sender: NSMenuItem) {
videoVariables.videoName = "Test_Video_2"
var theNewURL = Bundle.main.url(forResource:videoVariables.videoName, withExtension: "mp4")
player = AVPlayer.init(url: theNewURL!)
//!!this line crashes the app with the error "thread 1 exc_bad_instruction (code=exc_i386_invop subcode=0x0)" every time the menu item is being selected!!
playerView.player = player
}
}
また、背景ビデオがインタラクティブ(例:ユーザーをすることになっていません。
私のコードは次の通りである、アプリがクラッシュ行が一番下にあります)ので、ユーザーの対話性によって引き起こされる可能性のある問題は無視できます。アプリの目的は、コマンドを実行しているのとまったく同じ効果を作成し、ユーザーのデスクトップ上で動画を再生することです:
「/System/Library/Frameworks/ScreenSaver.framework/Resources/ ScreenSaverEngine.app/Contents/MacOS/ScreenSaverEngine -background "を端末に挿入します。
ご協力いただければ幸いです。