Skip to content

Commit 4598c82

Browse files
committed
Add handling for taking ownership when switching player mode
1 parent a14282f commit 4598c82

File tree

2 files changed

+72
-20
lines changed

2 files changed

+72
-20
lines changed

Assets/USharpVideo/Scripts/SyncModeController.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,20 +43,12 @@ public void SetStreamVisual()
4343

4444
public void ClickVideoToggle()
4545
{
46-
if (!Networking.IsOwner(videoPlayer.gameObject) ||
47-
videoPlayer.HasVideoSyncMode())
48-
return;
49-
50-
SetVideoVisual();
46+
videoPlayer.SetVideoSyncMode();
5147
}
5248

5349
public void ClickStreamToggle()
5450
{
55-
if (!Networking.IsOwner(videoPlayer.gameObject) ||
56-
videoPlayer.HasStreamSyncMode())
57-
return;
58-
59-
SetStreamVisual();
51+
videoPlayer.SetStreamSyncMode();
6052
}
6153
}
6254
}

Assets/USharpVideo/Scripts/USharpVideoPlayer.cs

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ private void OnDisable()
131131
screenRenderer.sharedMaterial.SetInt("_IsAVProInput", 0);
132132
}
133133

134+
void TakeOwnership()
135+
{
136+
if (Networking.IsMaster || !_masterOnly)
137+
{
138+
if (!Networking.IsOwner(gameObject))
139+
{
140+
Networking.SetOwner(Networking.LocalPlayer, gameObject);
141+
}
142+
}
143+
}
144+
134145
void StartVideoLoad(VRCUrl url)
135146
{
136147
Debug.Log("[USharpVideo] Started video load");
@@ -223,20 +234,39 @@ public void TriggerLockButton()
223234
masterUnlockedIcon.SetActive(!_masterOnly);
224235
}
225236

237+
// Pauses videos and stops streams
226238
public void TriggerPauseButton()
227239
{
228240
if (!Networking.IsOwner(gameObject))
229241
return;
230242

231243
_ownerPaused = !_ownerPaused;
232244

233-
if (_ownerPaused)
245+
if (currentPlayerMode == PLAYER_MODE_VIDEO ||
246+
currentPlayerMode == PLAYER_MODE_KARAOKE)
234247
{
235-
_currentPlayer.Pause();
236-
_locallyPaused = true;
248+
if (_ownerPaused)
249+
{
250+
_currentPlayer.Pause();
251+
_locallyPaused = true;
252+
}
253+
else
254+
_currentPlayer.Play();
237255
}
238256
else
239-
_currentPlayer.Play();
257+
{
258+
if (_ownerPaused)
259+
{
260+
_currentPlayer.Pause();
261+
_locallyPaused = true;
262+
}
263+
else
264+
{
265+
_currentPlayer.Play();
266+
_currentPlayer.SetTime(float.MaxValue);
267+
}
268+
269+
}
240270

241271
playIcon.SetActive(_ownerPaused);
242272
pauseIcon.SetActive(!_ownerPaused);
@@ -390,14 +420,36 @@ void UpdateVideoLoad()
390420
}
391421
}
392422

393-
public bool HasVideoSyncMode() => currentPlayerMode == PLAYER_MODE_VIDEO;
394-
public bool HasStreamSyncMode() => currentPlayerMode == PLAYER_MODE_STREAM;
423+
public void SetVideoSyncMode()
424+
{
425+
if (_masterOnly && !Networking.IsMaster)
426+
return;
427+
428+
TakeOwnership();
429+
430+
currentPlayerMode = PLAYER_MODE_VIDEO;
431+
432+
ChangePlayerMode();
433+
}
434+
435+
public void SetStreamSyncMode()
436+
{
437+
if (_masterOnly && !Networking.IsMaster)
438+
return;
439+
440+
TakeOwnership();
441+
442+
currentPlayerMode = PLAYER_MODE_STREAM;
443+
444+
ChangePlayerMode();
445+
}
395446

396447
void ChangePlayerMode()
397448
{
398449
if (currentPlayerMode == _localPlayerMode)
399450
return;
400-
451+
452+
_nextPlaylistIndex = -1;
401453
_currentPlayer.Stop();
402454
_locallyPaused = _ownerPaused = false;
403455

@@ -449,6 +501,9 @@ public override void OnDeserialization()
449501
{
450502
Debug.Log("[USharpVideo] Play");
451503
_currentPlayer.Play();
504+
if (currentPlayerMode == PLAYER_MODE_STREAM)
505+
_currentPlayer.SetTime(float.MaxValue);
506+
452507
_locallyPaused = false;
453508
}
454509

@@ -531,8 +586,8 @@ private void Update()
531586
}
532587
else // Stream player
533588
{
534-
if (!string.IsNullOrEmpty(_statusStr))
535-
SetStatusText("");
589+
//if (!string.IsNullOrEmpty(_statusStr))
590+
SetStatusText(currentTime.ToString());
536591

537592
screenRenderer.sharedMaterial.SetTexture("_EmissionMap", streamRTSource.sharedMaterial.GetTexture("_MainTex"));
538593
}
@@ -545,7 +600,12 @@ private void Update()
545600
_videoStartNetworkTime = (float)Networking.GetServerTimeInSeconds() - currentTime;
546601
}
547602

548-
_currentPlayer.Pause();
603+
if (currentPlayerMode == PLAYER_MODE_VIDEO ||
604+
currentPlayerMode == PLAYER_MODE_KARAOKE)
605+
_currentPlayer.Pause();
606+
else
607+
_currentPlayer.Pause();
608+
549609
_locallyPaused = true;
550610
}
551611

0 commit comments

Comments
 (0)