Stop单个Coroutine

public Coroutine StartCoroutine(string methodName, object value = null);

Description

Starts a coroutine named methodName.

In most cases you want to use the StartCoroutine variation above. However StartCoroutine using a string method name allows you to use StopCoroutine with a specific method name. The downside is that the string version has a higher runtime overhead to start the coroutine and you can pass only one parameter.

// In this example we show how to invoke a coroutine using a string name and stop it

function Start () { StartCoroutine("DoSomething", 2.0); yield WaitForSeconds(1); StopCoroutine("DoSomething"); }

function DoSomething (someParameter : float) { while (true) { print("DoSomething Loop"); // Yield execution of this coroutine and return to the main loop until next frame yield; } }
using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour { IEnumerator Start() { StartCoroutine("DoSomething", 2.0F); yield return new WaitForSeconds(1); StopCoroutine("DoSomething"); } IEnumerator DoSomething(float someParameter) { while (true) { print("DoSomething Loop"); yield return null; } } }
using RenderHeads.Media.AVProVideo; using System; using System.Collections; using System.Collections.Generic; using System.Runtime.CompilerServices; using UnityEngine; using UnityEngine.Video; public class AudioVideoMgr : MonoBehaviour { #region public const string BLUE_SIDE_BOND = "蓝方羁绊.mp4"; public const string RED_SIDE_BOND = "红方羁绊.mp4"; public const string SAGITTARIUS_ATTACK = "射手攻击.mp4"; public const string NORMAL_ATTACK = "普通攻击.mp4"; public const string TREATMENT = "治疗.mp4"; public const string SHIELD= "保护盾.mp4"; public const string WEEKLY_LEADERBOARD_POINTS = "周榜视频.mp4"; #endregion // 单例模式确保全局唯一访问点 public static AudioVideoMgr Instance { get; private set; } // AVPro Video 核心组件 [Header("AVPro Video 配置")] public MediaPlayer mediaPlayerAlpha; // 视频播放器预制体 public MediaPlayer mediaPlayerAlpha1; // 视频播放器预制体 public MediaPlayer mediaPlayerNoAlpha; // 视频播放器预制体 public MediaPlayer mediaPlayerNoAlpha1; // 视频播放器预制体 private MediaPlayer currentMediaPlayer; // 当前播放器 private int curruntIndex1 = 0; private int curruntIndex2 = 0; [SerializeField] private DisplayUGUI displayUGUI; // 视频显示组件 private DisplayUGUI currentDisplayUGUI; // 当前显示的视频播放器 private DisplayUGUI oldDisplayUGUI; // 当前显示的视频播放器 // 音频管理部分(保留原有音频功能) [Header("音频配置")] public AudioSource bgmSource; public AudioSource sfxSource; private Action onFinish; // 视频播放结束回调 private Action onNoLoopFinish; // 不循环视频播放结束回调 private void Awake() { if (Instance == null) { Instance = this; DontDestroyOnLoad(gameObject); } else { Destroy(gameObject); } } private void Start() { mediaPlayerNoAlpha.Events.AddListener(OnVideoEvent); mediaPlayerNoAlpha.Play(); mediaPlayerNoAlpha1.Events.AddListener(OnVideoEvent); mediaPlayerNoAlpha1.Play(); mediaPlayerAlpha.Events.AddListener(OnVideoEvent); mediaPlayerAlpha.Play(); mediaPlayerAlpha1.Events.AddListener(OnVideoEvent); mediaPlayerAlpha1.Play(); } /**************** 视频管理功能 ****************/ // 初始化视频播放器 //public MediaPlayer CreateVideoPlayer(string playerId) //{ // 播放普通不透明视频 public void PlayNormalVideo(string path,DisplayUGUI displayUGUI=null,bool isLoop=false,bool isUrl = false) { if (displayUGUI) { oldDisplayUGUI = currentDisplayUGUI; currentDisplayUGUI = displayUGUI; } else { currentDisplayUGUI = this.displayUGUI; } if (curruntIndex1 == 0) { PlayVideoInternal(path, mediaPlayerNoAlpha, isUrl); mediaPlayerNoAlpha.Loop = isLoop; curruntIndex1++; } else if(curruntIndex1 == 1) { PlayVideoInternal(path, mediaPlayerNoAlpha1, isUrl); mediaPlayerNoAlpha1.Loop = isLoop; curruntIndex1 =0; } } // 播放透明视频(可选择左右/上下分离) public void PlayTransparentVideo(string path, DisplayUGUI displayUGUI = null, bool isloop=false,bool isUrl = false ) { if (displayUGUI) { oldDisplayUGUI = currentDisplayUGUI; currentDisplayUGUI = displayUGUI; } else { currentDisplayUGUI = this.displayUGUI; } mediaPlayerAlpha.Loop = isloop; if (curruntIndex2 == 0) { PlayVideoInternal(path, mediaPlayerAlpha, isUrl); curruntIndex2++; } else if (curruntIndex2 == 1) { PlayVideoInternal(path, mediaPlayerAlpha1, isUrl); curruntIndex2 = 0; } } public void PlayTransparentVideo(string path, DisplayUGUI displayUGUI,Action action) { if (displayUGUI) { oldDisplayUGUI = currentDisplayUGUI; currentDisplayUGUI = displayUGUI; } else { currentDisplayUGUI = this.displayUGUI; } onNoLoopFinish=action; if (curruntIndex2 == 0) { PlayVideoInternal(path, mediaPlayerAlpha,false); curruntIndex2++; } else if (curruntIndex2 == 1) { PlayVideoInternal(path, mediaPlayerAlpha1,false); curruntIndex2 = 0; } } private Coroutine playVideoCoroutine; public void PlayTransparentVideo(string path, bool isplay ,DisplayUGUI displayUGUI = null, bool isloop = false, bool isUrl = false) { Debug.Log(mediaPlayerAlpha.Control.IsPlaying()); if (displayUGUI) { oldDisplayUGUI = currentDisplayUGUI; currentDisplayUGUI = displayUGUI; } else { currentDisplayUGUI = this.displayUGUI; } if (isplay) { if (playVideoCoroutine != null) { StopCoroutine(playVideoCoroutine); } playVideoCoroutine=StartCoroutine(PlayVideo(path, isloop)); return; } mediaPlayerAlpha.Loop = isloop; PlayVideoInternal(path, mediaPlayerAlpha, isUrl); } private void Update() { } IEnumerator PlayVideo(string path, bool isloop) { yield return new WaitForSeconds(0.3f); if (mediaPlayerAlpha.Control.IsPlaying()) { onFinish = () => { PlayerNextVideo(path, isloop); }; } else { PlayerNextVideo(path,isloop); } } void PlayerNextVideo(string path,bool isloop) { mediaPlayerAlpha.Loop = isloop; PlayVideoInternal(path, mediaPlayerAlpha,false); } // 内部播放逻辑 private void PlayVideoInternal(string path,MediaPlayer mediaPlayer, bool isUrl) { if (mediaPlayer == null) { Debug.LogError("视频播放器未初始化"); return; } StopVideo(); // 停止之前的视频 currentDisplayUGUI.CurrentMediaPlayer = null; // 清空当前播放器 currentDisplayUGUI.CurrentMediaPlayer= mediaPlayer; // 设置当前播放器 currentDisplayUGUI.gameObject.SetActive(false); // 设置播放源 MediaPath mediaPath = isUrl ? new MediaPath(path, MediaPathType.AbsolutePathOrURL) : new MediaPath(path, MediaPathType.RelativeToStreamingAssetsFolder); mediaPlayer.OpenMedia(mediaPath); mediaPlayer.Play(); currentMediaPlayer= mediaPlayer; currentDisplayUGUI.gameObject.SetActive(true); } // 视频事件回调 private void OnVideoEvent(MediaPlayer mp, MediaPlayerEvent.EventType eventType, ErrorCode error) { //Debug.Log("重播1"); if (mp.Loop) { EventMvpOnLoop eventMvpOnLoop = new EventMvpOnLoop(); EventSystem.Instance.Trigger(eventMvpOnLoop); } onNoLoopFinish?.Invoke(); onNoLoopFinish = null; switch (eventType) { case MediaPlayerEvent.EventType.FinishedPlaying: //ResumeBGM(); // 视频结束恢复背景音乐 SkillHandler.OnFinsh?.Invoke(); //SkillHandler.OnFinsh?.Invoke(); //SkillHandler.OnFinsh?.Invoke(); //SkillHandler.OnFinsh?.Invoke(); //SkillHandler.OnFinsh?.Invoke(); if (currentMediaPlayer != null) { currentDisplayUGUI.CurrentMediaPlayer = null; // 清空当前播放器 currentDisplayUGUI.gameObject.SetActive(false); // 隐藏视频显示组件 } onFinish?.Invoke(); onFinish = null; break; case MediaPlayerEvent.EventType.Error: Debug.LogError($"视频播放错误: {error}"); break; } } /**************** 音频管理功能 ****************/ // 保留原有音频功能并增强 public void SetVideoVolume(float volume, bool isAlphaVideo = true) { if (isAlphaVideo) { mediaPlayerAlpha.Control.SetVolume (Mathf.Clamp01(volume)); } else { mediaPlayerNoAlpha.Control.SetVolume(0); Debug.Log("设置普通视频音量" + volume); } } public void PlayBGM(AudioClip clip, float volume = 0.8f) { bgmSource.clip = clip; bgmSource.volume = volume; bgmSource.loop = true; bgmSource.Play(); } public void PauseBGM() => bgmSource.Pause(); public void ResumeBGM() => bgmSource.UnPause(); public void PlaySFX(AudioClip clip, float volume = 1f) { sfxSource.PlayOneShot(clip, volume); } /**************** 资源管理 ****************/ public void StopVideo() { //mediaPlayerAlpha.Stop(); //mediaPlayerNoAlpha.Stop(); //mediaPlayerNoAlpha1.Stop(); if (currentMediaPlayer != null) { currentMediaPlayer.Stop(); } if (currentDisplayUGUI != null) { currentDisplayUGUI.gameObject.SetActive(false); } if (oldDisplayUGUI != null) { oldDisplayUGUI.gameObject.SetActive(false); } displayUGUI.gameObject.SetActive(false); } public void StopVideoAll() { mediaPlayerAlpha.Stop(); mediaPlayerNoAlpha.Stop(); mediaPlayerNoAlpha1.Stop(); mediaPlayerAlpha1.Stop(); if (currentMediaPlayer != null) { currentMediaPlayer.Stop(); } } } 如何同时播放显示多个视频,有可能10个以上
最新发布
08-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值