Re: AVSimplePlayer in Swift?
Re: AVSimplePlayer in Swift?
- Subject: Re: AVSimplePlayer in Swift?
- From: Charles Jenkins <email@hidden>
- Date: Sat, 07 Jan 2017 16:24:36 -0500
I did try after you asked, and KVO may be working, but the video doesn’t
play. If I delete the KVO bindings from IB and change my code like this:
var currentTime : Double = 0 {
didSet {
timeSlider.doubleValue = currentTime
}
}
var duration: Double = 0
{
didSet {
timeSlider.maxValue = duration
}
}
The video will play and all the buttons will work right. Seriously,
*everything* works except I can’t scrub and I haven’t bothered yet to test
the volume slider.
If I take that working code and the ONLY changes I make are to (a) change
currentTime to the code below and (b) add the binding in IB, the video will
not play. The strange thing is when I do this, I think KVO is actually
working because I get hammered with log messages saying “Seek to 0.0
seconds.”
dynamic var currentTime : Double {
get {
return CMTimeGetSeconds( player.currentTime() )
}
set ( seconds ) {
NSLog( "Seek to \(seconds) seconds" )
let time = CMTimeMakeWithSeconds( seconds, 1 )
player.seek( to: time )
}
}
I thought maybe this meant my periodic time observer was messed up, so I
replaced my equivalent of the ObjC code with this:
myTimeObserver = player.addPeriodicTimeObserver(
forInterval: CMTimeMake( 1, 10 ),
queue: DispatchQueue.main
) {
[weak self] ( time: CMTime ) -> () in
if let weakSelf = self {
let seconds = weakSelf.currentTime // Force a read from player
NSLog( "Asking to set time to \(seconds) seconds" )
weakSelf.currentTime = seconds // Force KVO update
}
}
And the resulting log messages prove, I think, that player.currentTime()
really does sit at 0.0 while I’m using KVO for the slider.
On Sat, Jan 7, 2017 at 3:32 PM, Charles Srstka <email@hidden>
wrote:
> On Jan 7, 2017, at 2:27 PM, Charles Jenkins <email@hidden> wrote:
>
>
> Charles,
>
> Thank you for the reply. “Won’t compile” was incorrect shorthand for a
> long-winded explanation. What really happens is this:
>
> In the AVSimplePlayer demo, the timeSlider has two bindings: Value is
> bound to File’s Owner.currentTime and MaxValue is bound to File’s
> Owner.duration. These are double properties in ObjC. Here is an example of
> one of them:
>
> - (double)currentTime
> {
> return CMTimeGetSeconds(self.player.currentTime);
> }
>
> - (void)setCurrentTime:(double)time
> {
> [self.player seekToTime:CMTimeMakeWithSeconds(time, 1)
> toleranceBefore:kCMTimeZero toleranceAfter:kCMTimeZero];
> }
>
> In my project using Swift, I haven’t found a way to set File’s Owner in
> IB, so I choose my binding object to be “View Controller,” because Xcode
> automatically generated my view controller class with the name
> ViewController. I have the currentTime and duration properties in my Swift
> file, so I try to make my timeSlider mimic Apple’s two bindings: Value is
> bound to ViewController.currentTime and MaxValue is bound to
> ViewController.duration. I know I haven’t mistyped them because when I
> clear the Model Key Path boxes, I can use a pulldown to select the
> properties. But when I select them, a red stop sign appears in each Model
> Key Path box, and when I hover over it I see this message like this: “The
> Value binding expects to be bound to an object of type NSNumber, but
> currentTime is of type Double.”
>
> Here’s my updated code for the currentTime property IB doesn’t like:
>
> dynamic var currentTime: Double {
> get {
> return CMTimeGetSeconds( player.currentTime() )
> }
> set ( seconds ) {
> let time = CMTimeMakeWithSeconds( seconds, 1 )
> player.seek( to: time )
> }
> }
>
> To be clear, my ViewController object is an NSViewController subclass, so
> I think NSObject conformance is a given. What else could be wrong?
>
>
> Have you just tried compiling and running it and seeing if it works? I’ve
> found that little stop sign in the Model Key Path box to produce false
> positives from time to time, and this sounds like one. The KVO system wraps
> primitive types in objects, and for Double, that’ll be NSNumber. As long as
> your properties are dynamic, it should work.
>
> Charles
>
>
--
Charles
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden