Skip to content

Commit

Permalink
Merge pull request #305 from techanon/media-restart-fix
Browse files Browse the repository at this point in the history
Redundant Media Start Fix
  • Loading branch information
pema99 authored Apr 21, 2024
2 parents 04c70e1 + fbc2fb8 commit 7dff501
Showing 1 changed file with 28 additions and 20 deletions.
48 changes: 28 additions & 20 deletions Packages/com.llealloo.audiolink/Runtime/Scripts/ytdlpPlayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,16 @@ public void UpdateUrl(string resolved)
if (videoPlayer == null)
return;

videoPlayer.prepareCompleted -= MediaReady;
videoPlayer.prepareCompleted += MediaReady;
videoPlayer.url = resolved;
SetPlaybackTime(0.0f);
if (videoPlayer.length > 0)
{
videoPlayer.Play();
}
videoPlayer.Prepare();
}

private void MediaReady(VideoPlayer player)
{
SetPlaybackTime(player, 0.0f);
if (player.length > 0) player.Play();
}

public float GetPlaybackTime()
Expand All @@ -77,10 +81,10 @@ public float GetPlaybackTime()
return 0;
}

public void SetPlaybackTime(float time)
public void SetPlaybackTime(VideoPlayer player, float time)
{
if (videoPlayer != null && videoPlayer.length > 0 && videoPlayer.canSetTime)
videoPlayer.time = videoPlayer.length * Mathf.Clamp(time, 0.0f, 1.0f);
if (player != null && player.length > 0 && player.canSetTime)
player.time = player.length * Mathf.Clamp(time, 0.0f, 1.0f);
}

public string FormattedTimestamp(double seconds, double maxSeconds = 0)
Expand Down Expand Up @@ -261,14 +265,20 @@ private static string LocateExecutable(string name)
[CustomEditor(typeof(ytdlpPlayer))]
public class ytdlpPlayerEditor : UnityEditor.Editor
{
ytdlpPlayer _ytdlpPlayer;
private ytdlpPlayer _ytdlpPlayer;

private SerializedProperty ytdlpURL;
private SerializedProperty resolution;

void OnEnable()
{
_ytdlpPlayer = (ytdlpPlayer)target;
// If video player is on the same gameobject, assign it automatically
if (_ytdlpPlayer.gameObject.GetComponent<VideoPlayer>() != null)
_ytdlpPlayer.videoPlayer = _ytdlpPlayer.gameObject.GetComponent<VideoPlayer>();

ytdlpURL = serializedObject.FindProperty(nameof(_ytdlpPlayer.ytdlpURL));
resolution = serializedObject.FindProperty(nameof(_ytdlpPlayer.resolution));
}

public override bool RequiresConstantRepaint()
Expand All @@ -281,6 +291,7 @@ public override bool RequiresConstantRepaint()

public override void OnInspectorGUI()
{
serializedObject.Update();
#if UNITY_EDITOR_LINUX
bool available = false;
#else
Expand All @@ -296,13 +307,8 @@ public override void OnInspectorGUI()
using (new EditorGUILayout.HorizontalScope())
{
EditorGUILayout.LabelField(new GUIContent(" Video URL", EditorGUIUtility.IconContent("CloudConnect").image), GUILayout.Width(100));
EditorGUI.BeginChangeCheck();
_ytdlpPlayer.ytdlpURL = EditorGUILayout.TextField(_ytdlpPlayer.ytdlpURL);
if (EditorGUI.EndChangeCheck())
{
EditorUtility.SetDirty(_ytdlpPlayer);
};
_ytdlpPlayer.resolution = (ytdlpPlayer.Resolution)EditorGUILayout.EnumPopup(_ytdlpPlayer.resolution, GUILayout.Width(65));
EditorGUILayout.PropertyField(ytdlpURL, GUIContent.none);
EditorGUILayout.PropertyField(resolution, GUIContent.none, GUILayout.Width(65));
}

using (new EditorGUI.DisabledScope(!hasVideoPlayer || !EditorApplication.isPlaying))
Expand All @@ -327,7 +333,7 @@ public override void OnInspectorGUI()
EditorGUI.BeginChangeCheck();
playbackTime = GUILayout.HorizontalSlider(playbackTime, 0, 1);
if (EditorGUI.EndChangeCheck())
_ytdlpPlayer.SetPlaybackTime(playbackTime);
_ytdlpPlayer.SetPlaybackTime(_ytdlpPlayer.videoPlayer, playbackTime);

// Timestamp input
EditorGUI.BeginChangeCheck();
Expand All @@ -344,16 +350,16 @@ public override void OnInspectorGUI()
if (TimeSpan.TryParse($"00:{seekTimestamp}", out inputTimestamp))
{
playbackTime = (float)(inputTimestamp.TotalSeconds / videoLength);
_ytdlpPlayer.SetPlaybackTime(playbackTime);
_ytdlpPlayer.SetPlaybackTime(_ytdlpPlayer.videoPlayer, playbackTime);
}
}
}

// Media Controls
using (new EditorGUILayout.HorizontalScope())
{
bool isPlaying = hasVideoPlayer ? _ytdlpPlayer.videoPlayer.isPlaying : false;
bool isPaused = hasVideoPlayer ? _ytdlpPlayer.videoPlayer.isPaused : false;
bool isPlaying = hasVideoPlayer && _ytdlpPlayer.videoPlayer.isPlaying;
bool isPaused = hasVideoPlayer && _ytdlpPlayer.videoPlayer.isPaused;
bool isStopped = !isPlaying && !isPaused;

bool play = GUILayout.Toggle(isPlaying, new GUIContent(" Play", EditorGUIUtility.IconContent("d_PlayButton On").image), "Button") != isPlaying;
Expand Down Expand Up @@ -414,6 +420,8 @@ public override void OnInspectorGUI()
EditorGUILayout.HelpBox("Failed to locate yt-dlp executable. To fix this, install yt-dlp and make sure the executable is on your PATH. Once this is done, enter play mode to retry.", MessageType.Warning);
#endif
}

serializedObject.ApplyModifiedProperties();
}
}

Expand Down

0 comments on commit 7dff501

Please sign in to comment.