MediaPlayer_Subtitles.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. using UnityEngine;
  2. using System.Collections;
  3. //-----------------------------------------------------------------------------
  4. // Copyright 2015-2022 RenderHeads Ltd. All rights reserved.
  5. //-----------------------------------------------------------------------------
  6. namespace RenderHeads.Media.AVProVideo
  7. {
  8. public partial class MediaPlayer : MonoBehaviour
  9. {
  10. public bool EnableSubtitles(MediaPath mediaPath)
  11. {
  12. bool result = false;
  13. if (_subtitlesInterface != null)
  14. {
  15. if (mediaPath != null && !string.IsNullOrEmpty(mediaPath.Path))
  16. {
  17. string fullPath = mediaPath.GetResolvedFullPath();
  18. bool checkForFileExist = true;
  19. if (fullPath.Contains("://"))
  20. {
  21. checkForFileExist = false;
  22. }
  23. #if (!UNITY_EDITOR && UNITY_ANDROID)
  24. checkForFileExist = false;
  25. #endif
  26. if (checkForFileExist && !System.IO.File.Exists(fullPath))
  27. {
  28. Debug.LogError("[AVProVideo] Subtitle file not found: " + fullPath, this);
  29. }
  30. else
  31. {
  32. Helper.LogInfo("Opening subtitles " + fullPath, this);
  33. _previousSubtitleIndex = -1;
  34. try
  35. {
  36. if (fullPath.Contains("://"))
  37. {
  38. // Use coroutine and WWW class for loading
  39. if (_loadSubtitlesRoutine != null)
  40. {
  41. StopCoroutine(_loadSubtitlesRoutine);
  42. _loadSubtitlesRoutine = null;
  43. }
  44. _loadSubtitlesRoutine = StartCoroutine(LoadSubtitlesCoroutine(fullPath, mediaPath));
  45. }
  46. else
  47. {
  48. // Load directly from file
  49. string subtitleData = System.IO.File.ReadAllText(fullPath);
  50. if (_subtitlesInterface.LoadSubtitlesSRT(subtitleData))
  51. {
  52. _subtitlePath = mediaPath;
  53. _sideloadSubtitles = false;
  54. result = true;
  55. }
  56. else
  57. {
  58. Debug.LogError("[AVProVideo] Failed to load subtitles" + fullPath, this);
  59. }
  60. }
  61. }
  62. catch (System.Exception e)
  63. {
  64. Debug.LogError("[AVProVideo] Failed to load subtitles " + fullPath, this);
  65. Debug.LogException(e, this);
  66. }
  67. }
  68. }
  69. else
  70. {
  71. Debug.LogError("[AVProVideo] No subtitle file path specified", this);
  72. }
  73. }
  74. else
  75. {
  76. _queueSubtitlePath = mediaPath;
  77. }
  78. return result;
  79. }
  80. private IEnumerator LoadSubtitlesCoroutine(string url, MediaPath mediaPath)
  81. {
  82. UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Get(url);
  83. #if UNITY_2017_2_OR_NEWER
  84. yield return www.SendWebRequest();
  85. #else
  86. yield return www.Send();
  87. #endif
  88. string subtitleData = string.Empty;
  89. #if UNITY_2020_1_OR_NEWER
  90. if (www.result == UnityEngine.Networking.UnityWebRequest.Result.Success)
  91. #elif UNITY_2017_1_OR_NEWER
  92. if (!www.isNetworkError)
  93. #else
  94. if (!www.isError)
  95. #endif
  96. {
  97. subtitleData = ((UnityEngine.Networking.DownloadHandler)www.downloadHandler).text;
  98. }
  99. else
  100. {
  101. Debug.LogError("[AVProVideo] Error loading subtitles '" + www.error + "' from " + url);
  102. }
  103. if (_subtitlesInterface.LoadSubtitlesSRT(subtitleData))
  104. {
  105. _subtitlePath = mediaPath;
  106. _sideloadSubtitles = false;
  107. }
  108. else
  109. {
  110. Debug.LogError("[AVProVideo] Failed to load subtitles" + url, this);
  111. }
  112. _loadSubtitlesRoutine = null;
  113. www.Dispose();
  114. }
  115. public void DisableSubtitles()
  116. {
  117. if (_loadSubtitlesRoutine != null)
  118. {
  119. StopCoroutine(_loadSubtitlesRoutine);
  120. _loadSubtitlesRoutine = null;
  121. }
  122. if (_subtitlesInterface != null)
  123. {
  124. _previousSubtitleIndex = -1;
  125. _sideloadSubtitles = false;
  126. _subtitlesInterface.LoadSubtitlesSRT(string.Empty);
  127. }
  128. else
  129. {
  130. _queueSubtitlePath = null;
  131. }
  132. }
  133. }
  134. }