seturl.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using RenderHeads.Media.AVProVideo;
  4. using UnityEngine;
  5. public class seturl : MonoBehaviour
  6. {
  7. public List<MediaPlayer> mp;
  8. public List<GameObject> mpView;
  9. List<string> urls = new List<string>();
  10. int ct;
  11. int pl = 0;
  12. int hc = 1;
  13. Coroutine ctine;
  14. private void OnEnable()
  15. {
  16. ct = 0;
  17. ctine = StartCoroutine(playVieo());
  18. }
  19. private void OnDisable()
  20. {
  21. StopCoroutine(ctine);
  22. }
  23. IEnumerator playVieo()
  24. {
  25. while(true)
  26. {
  27. if(urls.Count>0)
  28. {
  29. if(mp.Count>1)
  30. {
  31. if (mp[pl] && mp[pl].Control != null && mp[pl].Control.IsPlaying())
  32. {
  33. // mp[pl].CloseMedia();
  34. yield return null;
  35. }
  36. if (!mpView[0].activeSelf && !mpView[1].activeSelf)
  37. {
  38. string url = urls[ct];
  39. // url = "https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/hls/xgplayer-demo.m3u8";
  40. MediaPath mpath = new MediaPath(url, MediaPathType.AbsolutePathOrURL);
  41. mp[pl].OpenMedia(mpath, true);
  42. mp[pl].Play();
  43. }
  44. int hcct = ct;
  45. hcct++;
  46. if (hcct >= urls.Count)
  47. {
  48. hcct = 0;
  49. }
  50. string url2 = urls[hcct];
  51. // url2 = "https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/hls/xgplayer-demo.m3u8";
  52. MediaPath mpath2 = new MediaPath(url2, MediaPathType.AbsolutePathOrURL);
  53. mp[hc].OpenMedia(mpath2, true);
  54. mp[hc].Play();
  55. mpView[pl].SetActive(true);
  56. mpView[hc].SetActive(true);
  57. int lspl = pl;
  58. pl = hc;
  59. hc = lspl;
  60. yield return new WaitForSeconds(20);
  61. while (!mp[pl].Control.IsPlaying())
  62. {
  63. mp[pl].Play();
  64. mpView[pl].SetActive(true);
  65. mpView[hc].SetActive(true);
  66. yield return new WaitForSeconds(2f);
  67. }
  68. ct++;
  69. if (ct >= urls.Count)
  70. {
  71. ct = 0;
  72. }
  73. }
  74. else
  75. {
  76. if (mp[0] && mp[0].Control != null && mp[0].Control.IsPlaying())
  77. {
  78. yield return null;
  79. }
  80. else
  81. {
  82. string url = urls[0];
  83. // url = "https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/hls/xgplayer-demo.m3u8";
  84. MediaPath mpath = new MediaPath(url, MediaPathType.AbsolutePathOrURL);
  85. mp[0].OpenMedia(mpath, true);
  86. mpView[0].SetActive(true);
  87. mp[0].Play();
  88. }
  89. while (!mp[pl].Control.IsPlaying())
  90. {
  91. mp[0].Play();
  92. yield return new WaitForSeconds(2f);
  93. }
  94. }
  95. }
  96. else
  97. {
  98. yield return null;
  99. }
  100. }
  101. }
  102. public void url(List<string> urls)
  103. {
  104. this.urls = urls;
  105. }
  106. public void url(string url)
  107. {
  108. mp[0].CloseMedia();
  109. this.urls = new List<string>();
  110. this.urls.Add(url);
  111. if(ctine!=null)
  112. StopCoroutine(ctine);
  113. ct = 0;
  114. ctine = StartCoroutine(playVieo());
  115. }
  116. }