function TMPVPlayer.Initialize: Boolean;
var
sl : TStringList;
i : Integer;
begin
if FInitialized then Exit;
FInitialized := False;
Result := False;
{$IFDEF WINDOWS}
if not IsLibMPV_Loaded then Exit;
{$ELSE}
FError := Load_libMPV(FMPVFileName);
if FError = MPV_ERROR_UNINITIALIZED then Exit;
{$ENDIF}
FMPV_HANDLE := mpv_create();
if not Assigned(FMPV_HANDLE) then
begin
FError := MPV_ERROR_UNSUPPORTED;
Free_libMPV;
Exit;
end;
// Get version lib
if Assigned(mpv_client_api_version) then
FVersion := mpv_client_api_version();
sl := TStringList.Create;
try
sl.Assign(FStartOptions);
if not FAutoStart then
sl.Add('pause'); // Start the player in paused state
if not FAutoLoadSub then
sl.Add('sub=no'); // don't load subtitles
if not FKeepAspect then
sl.Add('no-keepaspect'); // always stretch the video to window size
for i := 0 to sl.Count-1 do
mpv_set_option_string_(sl[i]);
finally
sl.Free;
end;
if not FYTDLPFileName.IsEmpty then
mpv_set_option_string(FMPV_HANDLE^, PChar('script-opts'), PChar('ytdl_hook-ytdl_path='+FYTDLPFileName));
// Set our window handle
if not SetWID then
begin
UnInitialize;
Exit;
end;
{$IFNDEF USETIMER}
mpv_observe_property(FMPV_HANDLE^, 0, 'playback-time', MPV_FORMAT_INT64);
{$ENDIF}
mpv_observe_property(FMPV_HANDLE^, 0, 'eof-reached', MPV_FORMAT_FLAG);
//mpv_observe_property(FMPV_HANDLE^, 0, 'paused-for-cache', MPV_FORMAT_INT64);
mpv_observe_property(FMPV_HANDLE^, 0, 'cache-buffering-state', MPV_FORMAT_INT64);
FError := mpv_initialize(FMPV_HANDLE^);
if FError <> MPV_ERROR_SUCCESS then
begin
UnInitialize;
Exit;
end;
FError := mpv_request_log_messages(FMPV_HANDLE^, PChar(LogLevelToString));
// Node text overlay cfg
FText := '';
SetLength(FTextNodeKeys, 4);
SetLength(FTextNodeValues, 4);
FTextNodeKeys[0] := 'name';
FTextNodeValues[0].format := MPV_FORMAT_STRING;
FTextNodeValues[0].u._string := 'osd-overlay';
FTextNodeKeys[1] := 'id';
FTextNodeValues[1].format := MPV_FORMAT_INT64;
FTextNodeValues[1].u.int64_ := 1;
FTextNodeKeys[2] := 'format';
FTextNodeValues[2].format := MPV_FORMAT_STRING;
FTextNodeKeys[3] := 'data';
FTextNodeValues[3].format := MPV_FORMAT_STRING;
FTextNodeValues[3].u._string := NIL;
FTextNodeList.num := 4;
FTextNodeList.keys := @FTextNodeKeys[0];
FTextNodeList.values := @FTextNodeValues[0];
FTextNode.format := MPV_FORMAT_NODE_MAP;
FTextNode.u.list := @FTextNodeList;
FMPVEvent := TMPVPlayerThreadEvent.Create;
FMPVEvent.OnEvent := @ReceivedEvent;
mpv_set_wakeup_callback(FMPV_HANDLE^, @LIBMPV_EVENT, Self);
if FRenderMode = rmOpenGL then
InitializeRenderGL
{$IFDEF SDL2}
else if FRenderMode = rmSDL2 then
InitializeRenderSDL
{$ENDIF};
FInitialized := True;
Result := True;
end;