AVPlayer View

I’ve looked through the forums here and have found almost nothing in relation to AV Player views… found numerous posts on QTkit, but it seems QTkit has been deprecated. All I am looking to do is automatically play a video within a window. Nothing fancy, and I don’t even need play/pause/stop/etc. controls. I do need the video to loop however. I’ve looked through some of the Apple documentation on AVPlayer but nothing’s working for me. Am I even headed in the right direction? Thanks!



property avPlayerView : missing value -- connected to AV Player View in IB

on applicationWillFinishLaunching_(aNotification)

        avPlayerView's assetWithURL_(path:to:movie)

end applicationWillFinishLaunching_


Hi,

either you create the view programmatically, set the frame and add to the window’s view
or you subclass NSView and add the methods described in the “Playing a Video File Using AVPlayerLayer” section.

PS: if you have the PlayerView subclass with the methods layerClass, player and setPlayer
write

set thePlayer to current application's AVPlayer's playerWithURL:videoURL
playerView's setPlayer:thePlayer
thePlayer's play()

Way over my head. Thanks for the response though! If I’m understanding correctly, there’s no ‘simple’ way to implement an AVPlayer View in the way a QTMovie View could’ve been done:


       tell current application's QTMovie to set {theMovie, theErr} to movieWithFile_error_(theMoviePath, reference)
       tell movieView
           setMovie_(theMovie)
           play_(me)
       end tell

EDIT:

Nevermind. Got it working with your snippet of code Stefan. Thank you!! Here’s what works for me:



script AppDelegate
	
    property parent : class "NSObject"
    
    property moviePlayer : missing value
    property playerView : missing value
	
    on applicationWillFinishLaunching_(aNotification)

        set videoURL to "file://path/to/movie"
        set videoURL to current application's class "NSURL"'s URLWithString_(videoURL)
    
        set moviePlayer to current application's AVPlayer's playerWithURL:videoURL
        playerView's setPlayer:moviePlayer
        moviePlayer's play()

    end applicationWillFinishLaunching_
	
end script

As there is no explicit video view object in Interface Builder for AVFoundation, no.

But it’s not too hard to create the subclass in Objective-C as described in the documentation and control the player with AppleScriptObjC

Hmm. Maybe I’m misunderstanding, but I got this working by dragging an AV Player View object (from the object library) into my window in IB, and then linking that as a referencing outlet to the playerView variable found in the code in my previous post.

Well there didn’t used to be one, but there is now…

Usually I’m a late adopter :wink:

Now where’s the fun in that? :lol:

A bit late, but here goes.

AVPlayers and AVAssets need to be inserted into an AVPlayer layer. The process is almost the same as a CALayer and a NSView ( contentView() , setWantsLayer_(true) , setFrame_(the frame size and location) , etc.

You refer to the layers and sublayers the same as any Core Animation Layer as well as any animated properties ( opacity, size , position etc. )

The difficult part is getting used to doing EVERY aspect of AVFoundation programatically. The upside is you will have a better understanding of NSView’s , Core Animation , NSWindows and a few other common classes that you would need to use in a project.

PS If you plan on using time functions, 99% are not accessible via ASOBJ. You will have to add a cocoa class ( many previous posts and examples )

Later

After putting this on hold for a while, I finally got back around to it and got it working.

Originally I was using AVKit and an AV Player View in my project, but I needed to support 10.7/10.8, so AV Player View/AVKit was not an option, as it is only available in 10.9. So I had gone back to using QTKit, but it’s deprecated. What I ended up doing was subclassing NSView and setting everything up manually.

I needed the video to loop, so I created a NSTimer to handle running the following:



property playerView : missing value -- Linked to custom view in IB

        set videoPath to (current application's NSBundle's mainBundle()'s bundlePath()'s stringByAppendingPathComponent_("Contents/Resources/aMovie.mov")) as text
        
        set videoURL to current application's NSURL's fileURLWithPath_(videoPath)
        
        set AVAsset to current application's AVURLAsset's alloc()'s initWithURL_options_(videoURL, missing value, missing value)
        
        set AVPlayer to current application's AVPlayer's alloc()'s initWithURL_(videoURL)
        
        set AVPlayerLayer to current application's AVPlayerLayer's playerLayerWithPlayer_(AVPlayer)
        
        AVPlayerLayer's setFrame_(playerView's |bounds|())
        
        set AVPlayerItem to current application's AVPlayerItem's playerItemWithAsset_(AVAsset)
        
        AVPlayer's replaceCurrentItemWithPlayerItem_(AVPlayerItem)
        
        playerView's setWantsLayer_(true)
        
        AVPlayerLayer's setHidden_(false)

        playerView's layer()'s addSublayer_(AVPlayerLayer)
        
        AVPlayer's play()


Thanks everyone for your help!