seturl.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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(false);
  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. yield return new WaitForSeconds(2f);
  65. }
  66. ct++;
  67. if (ct >= urls.Count)
  68. {
  69. ct = 0;
  70. }
  71. }
  72. else
  73. {
  74. if (mp[0] && mp[0].Control != null && mp[0].Control.IsPlaying())
  75. {
  76. yield return null;
  77. }
  78. else
  79. {
  80. string url = urls[0];
  81. // url = "https://sf1-cdn-tos.huoshanstatic.com/obj/media-fe/xgplayer_doc_video/hls/xgplayer-demo.m3u8";
  82. MediaPath mpath = new MediaPath(url, MediaPathType.AbsolutePathOrURL);
  83. mp[0].OpenMedia(mpath, true);
  84. mpView[0].SetActive(true);
  85. mp[0].Play();
  86. }
  87. while (!mp[pl].Control.IsPlaying())
  88. {
  89. mp[0].Play();
  90. yield return new WaitForSeconds(2f);
  91. }
  92. }
  93. }
  94. else
  95. {
  96. yield return null;
  97. }
  98. }
  99. }
  100. public void url(List<string> urls)
  101. {
  102. this.urls = urls;
  103. }
  104. public void url(string url)
  105. {
  106. mp[0].CloseMedia();
  107. this.urls = new List<string>();
  108. this.urls.Add(url);
  109. }
  110. }