public void Initialize(NSView view, string videoFileName, EventHandler onVideoLoaded, EventHandler onVideoEnded) { if (string.IsNullOrEmpty(videoFileName)) { return; } string libFile = GetVlcLibFullPath(); string pluginPath = System.IO.Directory.GetParent(libFile).Parent.FullName; if (!System.IO.File.Exists(libFile)) { throw new Exception("VLC application not found!"); } Environment.SetEnvironmentVariable("VLC_PLUGIN_PATH", pluginPath); var dir = System.IO.Path.GetDirectoryName(pluginPath); System.IO.Directory.SetCurrentDirectory(dir); _libVlcDLL = dlopen(libFile, 0); LoadLibVlcDynamic(); if (!IsAllMethodsLoaded()) { throw new Exception("Not all methods from libvlc loaded!"); } string[] initParameters = { "--no-sub-autodetect-file" }; _libVlc = _libvlc_new(initParameters.Length, initParameters); if (_libVlc == IntPtr.Zero) { throw new Exception("Unable to call 'libvlc_new' - check VLC installation + plugin folder"); } var bytes = Encoding.UTF8.GetBytes(videoFileName + "\0"); IntPtr media = _libvlc_media_new_path(_libVlc, bytes); _mediaPlayer = _libvlc_media_player_new_from_media(media); _libvlc_media_release(media); _libvlc_media_player_set_nsobject(_mediaPlayer, view.Handle); _libvlc_media_player_play(_mediaPlayer); OnVideoLoaded = onVideoLoaded; _videoLoadedTimer = new Timer(100); _videoLoadedTimer.Elapsed += (object sender, ElapsedEventArgs e) => { view.InvokeOnMainThread(() => { if (IsPlaying) { _videoLoadedTimer.Stop(); Pause(); if (OnVideoLoaded != null) { OnVideoLoaded.Invoke(_mediaPlayer, new EventArgs()); } } }); }; _videoLoadedTimer.Start(); OnVideoEnded = onVideoEnded; _videoEndTimer = new Timer(250); _videoEndTimer.Elapsed += (object sender, ElapsedEventArgs e) => { view.InvokeOnMainThread(() => { const int Ended = 6; if (_libVlc == IntPtr.Zero || _mediaPlayer == IntPtr.Zero || _closing) { return; } int state = _libvlc_media_player_get_state(_mediaPlayer); if (state == Ended) { // hack to make sure VLC is in ready state Stop(); WaitForReady(); if (_libVlc == IntPtr.Zero || _mediaPlayer == IntPtr.Zero || _closing) { return; } Play(); Pause(); if (OnVideoEnded != null) { OnVideoEnded.Invoke(_mediaPlayer, new EventArgs()); } } }); }; _videoEndTimer.Start(); }