Search code examples
cswiftmpv

libmpv intptr_t of an NSView


I am trying to use libmpv in a swift project. I have everything linked up and am trying to follow the basic example.

The problem is, that to show a video, I have to pass an integer pointer (intptr_t) to the wid command. In Objective-C this would be done as the example above hints:

int64_t wid = (intptr_t) self->wrapper;

where wrapper is the wrapper view meant for the video player. However, as swift is not meant to be working with pointers, and the wrapper view is one as I understand, I do not know how to translate this to swift.

I tried casting the NSView to an UnsafePointer like this

let t = UnsafePointer(&view)

and then cast t to an intptr_t or Int but this conversion does not work. It says this value cannot be converted to the Int. So how can I embed the video player in a swift view?


Solution

  • It doesn't look like you need an int pointer, at all. The mpv_set_option() API you're trying to call actually takes a void *. In Swift, this is bridged as UnsafeRawPointer.

    I think you can just pass in &view directly, which should bridge to UnsafeRawPointer automatically. However, now you have to be careful, because you have to ensure that the lifetime your VC lasts at least as long as mpv needs it.