MusicMgr.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class MusicMgr : SingletonMono<MusicMgr> {
  5. public static string music_battle_default = "";
  6. public static string music_main_city = "";
  7. public static string music_battle = "";
  8. private AudioListener m_currentListener = null;
  9. private GameObject mBgMusic = null;
  10. private GameObject mSound = null;
  11. private HashSet<string> mNoClearSoundNameSet = new HashSet<string> ();
  12. /// <summary>
  13. /// 跳场景不清除的声音
  14. /// </summary>
  15. private List<AudioSource> mNoClearSoundList = new List<AudioSource> ();
  16. private List<AudioSource> mSoundList = new List<AudioSource> ();
  17. private Dictionary<string, AudioSource> mMusicList = new Dictionary<string, AudioSource> ();
  18. private Dictionary<string, AudioClip> mAudioClipList = new Dictionary<string, AudioClip> ();
  19. private Dictionary<string, AudioClip> mMusicClipList = new Dictionary<string, AudioClip> ();
  20. private Dictionary<string, bool> mLoadingAudioLoopInfo = new Dictionary<string, bool> ();
  21. HashSet<string> mLatestPlaySoundSet = new HashSet<string> ();
  22. Dictionary<string,bool> mLoadingFightSounds = new Dictionary<string, bool> ();
  23. private float mSoundVolume = 1f;
  24. private float mMusicVolume = 0.6f;
  25. private float m_UpdateCD = 0f;
  26. private bool mIsPlaySoundEffect = true;
  27. private bool mIsPlayBgMusic = true;
  28. private bool mInternalMuteMusic = true;
  29. private string mCurrentMusicName = "";
  30. private string mPreviousMusicName = "";
  31. private bool mLoading = false;
  32. public AudioListener Listener {
  33. get {
  34. return m_currentListener;
  35. }
  36. }
  37. public bool MuteMusic {
  38. get { return !mIsPlayBgMusic; }
  39. set {
  40. value = !value;
  41. if (mIsPlayBgMusic != value) {
  42. mIsPlayBgMusic = value;
  43. ExcuteMuteMusic();
  44. GameSettings.Instance.MusicMute = !value;
  45. }
  46. }
  47. }
  48. public bool InternalMuteMusic {
  49. get { return mInternalMuteMusic; }
  50. set {
  51. if (mInternalMuteMusic == value) return;
  52. mInternalMuteMusic = value;
  53. ExcuteMuteMusic();
  54. }
  55. }
  56. private void ExcuteMuteMusic()
  57. {
  58. Dictionary<string, AudioSource>.Enumerator iter = mMusicList.GetEnumerator ();
  59. while (iter.MoveNext ()) {
  60. iter.Current.Value.mute = (MuteMusic || InternalMuteMusic);
  61. }
  62. iter.Dispose ();
  63. }
  64. public bool MuteSound {
  65. get { return !mIsPlaySoundEffect; }
  66. set {
  67. value = !value;
  68. if (mIsPlaySoundEffect != value) {
  69. mIsPlaySoundEffect = value;
  70. List<AudioSource>.Enumerator iter = mSoundList.GetEnumerator ();
  71. while (iter.MoveNext ()) {
  72. iter.Current.mute = (!mIsPlaySoundEffect);
  73. }
  74. iter.Dispose ();
  75. iter = mNoClearSoundList.GetEnumerator ();
  76. while (iter.MoveNext ()) {
  77. iter.Current.mute = (!mIsPlaySoundEffect);
  78. }
  79. iter.Dispose ();
  80. GameSettings.Instance.SoundMute = !value;
  81. }
  82. }
  83. }
  84. public float SoundVolume {
  85. get {
  86. return mSoundVolume;
  87. }
  88. set {
  89. if (mSoundVolume != value) {
  90. mSoundVolume = Mathf.Clamp01 (value); // Mathf.Max(Mathf.Min(value, 1.0f), 0);
  91. List<AudioSource>.Enumerator iter = mSoundList.GetEnumerator ();
  92. while (iter.MoveNext ()) {
  93. iter.Current.volume = mSoundVolume;
  94. iter.Current.mute = mSoundVolume < 0.01f;
  95. }
  96. iter = mNoClearSoundList.GetEnumerator ();
  97. while (iter.MoveNext ()) {
  98. iter.Current.volume = mSoundVolume;
  99. iter.Current.mute = mSoundVolume < 0.01f;
  100. }
  101. GameSettings.Instance.SoundVolume = mSoundVolume;
  102. }
  103. }
  104. }
  105. public float MusicVolume {
  106. get {
  107. return mMusicVolume;
  108. }
  109. set {
  110. if (mMusicVolume != value) {
  111. mMusicVolume = Mathf.Clamp01 (value); // Mathf.Max(Mathf.Min(value, 1.0f), 0);
  112. Dictionary<string, AudioSource>.Enumerator iter = mMusicList.GetEnumerator ();
  113. while (iter.MoveNext ()) {
  114. iter.Current.Value.volume = mMusicVolume;
  115. iter.Current.Value.mute = mMusicVolume < 0.01f;
  116. }
  117. GameSettings.Instance.MusicVolume = mMusicVolume;
  118. }
  119. }
  120. }
  121. public string CurrentMusicName {
  122. get { return mCurrentMusicName; }
  123. }
  124. public override void InitMgr () {
  125. base.InitMgr ();
  126. Init ();
  127. }
  128. protected override void Dispose () {
  129. base.Dispose ();
  130. Clean ();
  131. SaveSetting ();
  132. }
  133. void Init () {
  134. GameObject bg_music = new GameObject ();
  135. bg_music.name = "MusicMgr_BgMusic";
  136. bg_music.transform.SetParent (this.transform);
  137. mBgMusic = bg_music;
  138. GameObject sound = new GameObject ();
  139. sound.name = "MusicMgr_Sound";
  140. sound.transform.SetParent (this.transform);
  141. mSound = sound;
  142. AudioListener listener = UnityEngineUtils.GetComponent<AudioListener> (this, true);
  143. if (listener != null)
  144. m_currentListener = listener;
  145. AudioListener.volume = 1.0f;
  146. AudioAttach.ProxyMode = true;
  147. mIsPlayBgMusic = !GameSettings.Instance.MusicMute;
  148. mIsPlaySoundEffect = !GameSettings.Instance.SoundMute;
  149. mSoundVolume = GameSettings.Instance.SoundVolume;
  150. mMusicVolume = GameSettings.Instance.MusicVolume;
  151. }
  152. public long PreloadFightSound (string[] sounds) {
  153. //数据不存在,直接返回
  154. if (sounds == null || sounds.Length == 0)
  155. return 0;
  156. List<string> needLoadAssets = new List<string> ();
  157. for (int i = 0; i < sounds.Length; ++i) {
  158. if (string.IsNullOrEmpty (sounds[i]))
  159. continue;
  160. if (mAudioClipList.ContainsKey (sounds[i]))
  161. continue;
  162. if (mLoadingFightSounds.ContainsKey (sounds[i]))
  163. continue;
  164. if (sounds[i] == "none")
  165. continue;
  166. mLoadingFightSounds.Add (sounds[i],false);
  167. needLoadAssets.Add (sounds[i]);
  168. }
  169. if (needLoadAssets.Count > 0) {
  170. return ResourceMgr.Instance.LoadAsset<List<AudioClip>> (OnSoundPreLoaded, Constants.FightAudioPath, needLoadAssets.ToArray ());
  171. }
  172. return 0;
  173. }
  174. public void PreloadUISound (string[] resAssets) {
  175. List<string> needLoadAssets = new List<string> ();
  176. for (int i = 0; i < resAssets.Length; ++i) {
  177. if (mAudioClipList.ContainsKey (resAssets[i])) continue;
  178. needLoadAssets.Add (resAssets[i]);
  179. }
  180. if (needLoadAssets.Count > 0) {
  181. ResourceMgr.Instance.LoadAsset<List<AudioClip>> (OnSoundPreLoaded, Constants.UIAudioPath, needLoadAssets.ToArray ());
  182. }
  183. }
  184. public void PlayFightSound (string soundName, bool repeat = false) {
  185. PlaySound (Constants.FightAudioPath, soundName, repeat);
  186. }
  187. public void PlayUISound (string name, bool removeall) {
  188. PlayUISound (name, removeall, false);
  189. }
  190. public void PlayUISound (string name, bool removeall, bool noclear) {
  191. if (string.IsNullOrEmpty (name)) return;
  192. if (noclear && !mNoClearSoundNameSet.Contains (name))
  193. mNoClearSoundNameSet.Add (name);
  194. AudioClip clip = null;
  195. if (mAudioClipList.TryGetValue (name, out clip)) {
  196. PlaySound (clip, removeall, false, noclear);
  197. return;
  198. }
  199. //! should check ui-sound...
  200. ResourceMgr.Instance.LoadAsset<AudioClip> (OnSoundLoaded, Constants.UIAudioPath, name);
  201. }
  202. public void PlaySound (string path, string audioName,bool repeat = false) {
  203. if (string.IsNullOrEmpty (audioName) || string.Compare (audioName, "none", true) == 0)
  204. return;
  205. if (mLatestPlaySoundSet.Contains (audioName))
  206. return;
  207. mLatestPlaySoundSet.Add (audioName);
  208. AudioClip clip = null;
  209. if (mAudioClipList.TryGetValue (audioName, out clip)) {
  210. PlaySound (clip, false, repeat);
  211. return;
  212. }
  213. if (mLoadingFightSounds.ContainsKey (audioName))
  214. return;
  215. mLoadingFightSounds.Add (audioName, repeat);
  216. ResourceMgr.Instance.LoadAsset<AudioClip> (OnSoundLoaded, path, audioName);
  217. }
  218. public void PlayBGMusic (string bgmName) {
  219. PlayBGMusicWithLoop (bgmName, true);
  220. }
  221. //同时只能有一个背景音乐播放
  222. Coroutine mCurrMusicPlayer = null;
  223. private bool mbFadeInning = false;
  224. //
  225. public void PlayBGMusicWithLoop (string resName, bool isLoop) {
  226. if (mBgMusic == null || string.IsNullOrEmpty (resName) || resName.ToLower () == "none") return;
  227. //! Already Playing...
  228. if (mMusicList.ContainsKey (resName)) return;
  229. mPreviousMusicName = mCurrentMusicName;
  230. mCurrentMusicName = resName;
  231. AudioClip clip = null;
  232. if (mMusicClipList.TryGetValue (resName, out clip)) {
  233. if (mCurrMusicPlayer != null) {
  234. StopCoroutine (mCurrMusicPlayer);
  235. mCurrMusicPlayer = null;
  236. }
  237. mCurrMusicPlayer = StartCoroutine (PlayMusicCallBack (clip, isLoop));
  238. return;
  239. }
  240. if (!mLoadingAudioLoopInfo.ContainsKey (resName)) {
  241. mLoadingAudioLoopInfo.Add (resName, isLoop);
  242. ResourceMgr.Instance.LoadAsset<AudioClip> (OnMusicLoaded, Constants.BGMAudioPath, resName);
  243. }
  244. }
  245. private IEnumerator PlayMusicCallBack (AudioClip clip, bool isLoop = true) {
  246. if (clip != null) {
  247. AudioSource audioSrc = null;
  248. Coroutine stopCor = null;
  249. Dictionary<string, AudioSource>.Enumerator iter = mMusicList.GetEnumerator ();
  250. while (iter.MoveNext ()) {
  251. audioSrc = iter.Current.Value;
  252. stopCor = StartCoroutine (StopMusic (audioSrc, true, 0.5f));
  253. }
  254. iter.Dispose ();
  255. mMusicList.Clear ();
  256. yield return stopCor;
  257. stopCor = null;
  258. yield return 1; //wait for one frame...
  259. audioSrc = mBgMusic.GetComponent<AudioSource> ();
  260. while (audioSrc != null) {
  261. yield return 1;
  262. audioSrc = mBgMusic.GetComponent<AudioSource> ();
  263. }
  264. audioSrc = mBgMusic.AddComponent<AudioSource> ();
  265. if (audioSrc != null) {
  266. audioSrc.playOnAwake = false;
  267. audioSrc.clip = clip;
  268. audioSrc.loop = isLoop;
  269. audioSrc.volume = mMusicVolume;
  270. audioSrc.mute = MuteMusic;
  271. StartCoroutine (FadeInPlay (1.2f, audioSrc));
  272. }
  273. mMusicList.Add (clip.name, audioSrc);
  274. }
  275. yield return 1;
  276. mCurrMusicPlayer = null;
  277. }
  278. IEnumerator StopMusic (AudioSource arc, bool isFadeOut, float fFadeOutTime) {
  279. if (arc != null) {
  280. yield return StartCoroutine (FadeOutDelete (fFadeOutTime, arc));
  281. //删除上一个播放的背景音乐
  282. if (mPreviousMusicName != "") {
  283. AudioClip clip;
  284. if (mMusicClipList.TryGetValue (mPreviousMusicName, out clip)) {
  285. if (clip != null)
  286. clip.UnloadAudioData ();
  287. mMusicClipList.Remove (mPreviousMusicName);
  288. }
  289. }
  290. }
  291. }
  292. IEnumerator FadeOutDelete (float fTime, AudioSource arc) {
  293. TweenVolume tween = TweenVolume.Begin (mBgMusic, fTime, 0f);
  294. //! wait TweenVoLume to end.
  295. if (tween) {
  296. while (tween.enabled)
  297. yield return 1;
  298. }
  299. if (arc != null) {
  300. arc.Stop ();
  301. if (tween) {
  302. tween.Destroy ();
  303. yield return 1;
  304. }
  305. arc.Destroy ();
  306. }
  307. }
  308. IEnumerator FadeInPlay (float fTime, AudioSource arc) {
  309. if (arc != null) {
  310. arc.volume = 0.01f;
  311. arc.Play ();
  312. mbFadeInning = true;
  313. TweenVolume tv = TweenVolume.Begin (mBgMusic, fTime, mMusicVolume);
  314. //! wait TweenVoLume to end.
  315. if (tv) {
  316. while (tv.enabled)
  317. yield return 1;
  318. }
  319. mbFadeInning = false;
  320. }
  321. }
  322. public void PlaySound (AudioClip clip, bool removeall = false, bool repeat = false, bool noclear = false) {
  323. if (clip != null) {
  324. if (removeall) {
  325. RemoveAllSound ();
  326. }
  327. AudioSource audioSrc = mSound.AddComponent<AudioSource> ();
  328. audioSrc.playOnAwake = false;
  329. audioSrc.clip = clip;
  330. audioSrc.loop = repeat;
  331. audioSrc.volume = mSoundVolume;
  332. audioSrc.mute = MuteSound;
  333. audioSrc.rolloffMode = AudioRolloffMode.Linear;
  334. audioSrc.Play ();
  335. if (noclear)
  336. mNoClearSoundList.Add (audioSrc);
  337. else
  338. mSoundList.Add (audioSrc);
  339. }
  340. }
  341. public void RemoveAllSound () {
  342. for (int i = mSoundList.Count - 1; i >= 0; --i) {
  343. AudioSource Aus = mSoundList[i];
  344. if (Aus != null) {
  345. Aus.Destroy ();
  346. }
  347. }
  348. mSoundList.Clear ();
  349. }
  350. public void Update () {
  351. if (m_UpdateCD + 1f < Time.realtimeSinceStartup) {
  352. m_UpdateCD = Time.realtimeSinceStartup;
  353. for (int i = mSoundList.Count - 1; i >= 0; --i) {
  354. if (mSoundList[i] == null || !mSoundList[i].isPlaying) {
  355. mAudioClipList.Remove (mSoundList[i].name);
  356. mSoundList[i].Destroy ();
  357. mSoundList.RemoveAt (i);
  358. }
  359. }
  360. for (int i = mNoClearSoundList.Count - 1; i >= 0; --i) {
  361. AudioSource asource = mNoClearSoundList[i];
  362. if (asource == null || !asource.isPlaying) {
  363. if (asource != null) {
  364. mAudioClipList.Remove (asource.name);
  365. asource.Destroy ();
  366. }
  367. mNoClearSoundList.RemoveAt (i);
  368. }
  369. }
  370. }
  371. }
  372. void FixedUpdate () {
  373. mLatestPlaySoundSet.Clear ();
  374. }
  375. public void RemoveSound (string name) {
  376. for (int idx = 0; idx < mSoundList.Count; idx++) {
  377. AudioSource a = mSoundList[idx];
  378. if (a.clip.name == name) {
  379. mSoundList.Remove (a);
  380. a.Destroy ();
  381. return;
  382. }
  383. }
  384. for (int idx = 0; idx < mNoClearSoundList.Count; idx++) {
  385. AudioSource a = mNoClearSoundList[idx];
  386. if (a.clip.name == name) {
  387. mNoClearSoundList.Remove (a);
  388. a.Destroy ();
  389. return;
  390. }
  391. }
  392. }
  393. public void SaveSetting () {
  394. GameSettings.Instance.Save ();
  395. }
  396. public void Clean () {
  397. if (mAudioClipList != null)
  398. mAudioClipList.Clear ();
  399. if (mMusicClipList != null)
  400. mMusicClipList.Clear ();
  401. }
  402. public void StopAllMusic () {
  403. if (mBgMusic != null) {
  404. TweenVolume twv = mBgMusic.GetComponent<TweenVolume> ();
  405. if (twv) Destroy (twv);
  406. }
  407. AudioSource audioSrc = null;
  408. Dictionary<string, AudioSource>.Enumerator iter = mMusicList.GetEnumerator ();
  409. while (iter.MoveNext ()) {
  410. audioSrc = iter.Current.Value;
  411. audioSrc.volume = 0f;
  412. StartCoroutine (StopMusic (audioSrc, false, 0.0f));
  413. }
  414. iter.Dispose ();
  415. mMusicList.Clear ();
  416. }
  417. /// <summary>
  418. //跳转场景时清理声音
  419. /// </summary>
  420. public void CleanBeforeChangeScene () {
  421. CleanFightSound ();
  422. CleanBGMusic ();
  423. }
  424. Stack<string> removeDict = new Stack<string> ();
  425. public void CleanFightSound () {
  426. AudioProxy.Instance.audioClips.Clear ();
  427. AudioProxy.Instance.cancelClips.Clear ();
  428. //删除战斗和UI声音
  429. for (int idx = 0; idx < mSoundList.Count; idx++) {
  430. GameObject.DestroyObject (mSoundList[idx]);
  431. }
  432. mSoundList.Clear ();
  433. foreach (var audioCP in mAudioClipList) {
  434. AudioClip clip = audioCP.Value;
  435. if (CanClear (clip)) {
  436. clip.UnloadAudioData ();
  437. removeDict.Push (audioCP.Key);
  438. }
  439. }
  440. while (removeDict.Count > 0) {
  441. mAudioClipList.Remove (removeDict.Pop ());
  442. }
  443. }
  444. /// <summary>
  445. /// 清理背景音乐
  446. /// </summary>
  447. public void CleanBGMusic () {
  448. AudioClip curClip = null;
  449. foreach (var musicCP in mMusicClipList) {
  450. AudioClip clip = musicCP.Value;
  451. if (clip != null)
  452. clip.UnloadAudioData();
  453. }
  454. mMusicClipList.Clear ();
  455. }
  456. bool CanClear (AudioClip clip) {
  457. if (mNoClearSoundList != null && mNoClearSoundList.Count > 0) {
  458. for (int idx = 0; idx < mNoClearSoundList.Count; idx++) {
  459. if (mNoClearSoundList[idx].clip == clip)
  460. return false;
  461. }
  462. }
  463. return true;
  464. }
  465. AudioSource PlayUISound (AudioClip clip, float volume, bool isLoop) {
  466. if (clip != null) {
  467. AudioSource[] audioList = mSound.GetComponents<AudioSource> ();
  468. for (int i = 0; i < audioList.Length; ++i) {
  469. AudioSource src = audioList[i];
  470. if (!src.isPlaying) {
  471. src.Stop ();
  472. src.Destroy ();
  473. }
  474. }
  475. AudioSource audioSrc = mSound.AddComponent<AudioSource> ();
  476. if (audioSrc != null) {
  477. audioSrc.outputAudioMixerGroup = null;
  478. audioSrc.playOnAwake = false;
  479. audioSrc.clip = clip;
  480. audioSrc.loop = isLoop;
  481. audioSrc.rolloffMode = AudioRolloffMode.Linear;
  482. audioSrc.volume = volume * mSoundVolume;
  483. audioSrc.mute = MuteSound;
  484. audioSrc.Play ();
  485. }
  486. return audioSrc;
  487. }
  488. return null;
  489. }
  490. void StopUISound (AudioClip clip) {
  491. AudioSource[] audioList = mSound.GetComponents<AudioSource> ();
  492. for (int i = 0; i < audioList.Length; ++i) {
  493. AudioSource src = audioList[i];
  494. if (src != null && src.clip == clip) {
  495. src.Stop ();
  496. src.Destroy ();
  497. }
  498. }
  499. }
  500. void OnSoundPreLoaded (List<AudioClip> audioClips, string assetPath, params string[] assetNames) {
  501. mLoading = false;
  502. if (audioClips == null) return;
  503. for (int i = 0; i < audioClips.Count; ++i) {
  504. if (audioClips[i] == null)
  505. continue;
  506. string clipName = assetNames[i];
  507. audioClips[i].name = clipName;
  508. if (!mAudioClipList.ContainsKey (clipName))
  509. mAudioClipList.Add (clipName, audioClips[i]);
  510. if (mLoadingFightSounds.ContainsKey (clipName))
  511. mLoadingFightSounds.Remove (clipName);
  512. }
  513. }
  514. void OnSoundLoaded (AudioClip audioClip, string assetPath, params string[] assetName) {
  515. if (audioClip == null) return;
  516. string clipName = assetName[0];
  517. if (!mAudioClipList.ContainsKey (clipName)) {
  518. bool repeat = false;
  519. mLoadingFightSounds.TryGetValue(clipName, out repeat);
  520. bool noClear = mNoClearSoundNameSet.Contains (clipName);
  521. audioClip.name = clipName;
  522. mAudioClipList.Add (clipName, audioClip);
  523. PlaySound (audioClip, false, repeat, noClear);
  524. }
  525. if (mLoadingFightSounds.ContainsKey (clipName))
  526. mLoadingFightSounds.Remove (clipName);
  527. }
  528. void OnMusicLoaded (AudioClip audioClip, string assetPath, params string[] assetName) {
  529. if (audioClip != null) {
  530. string audioName = audioClip.name;
  531. if (!mMusicClipList.ContainsKey (audioName)) {
  532. mMusicClipList.Add (audioName, audioClip);
  533. } else {
  534. DebugHelper.LogWarning ("The audio {0} is existing", audioName);
  535. }
  536. if (mLoadingAudioLoopInfo.ContainsKey (audioName)) {
  537. bool isLoop = mLoadingAudioLoopInfo[audioName];
  538. mLoadingAudioLoopInfo.Remove (audioName);
  539. if (mCurrMusicPlayer != null) {
  540. StopCoroutine (mCurrMusicPlayer);
  541. mCurrMusicPlayer = null;
  542. }
  543. mCurrMusicPlayer = StartCoroutine (PlayMusicCallBack (audioClip, isLoop));
  544. } else {
  545. if (mCurrMusicPlayer != null) {
  546. StopCoroutine (mCurrMusicPlayer);
  547. mCurrMusicPlayer = null;
  548. }
  549. mCurrMusicPlayer = StartCoroutine (PlayMusicCallBack (audioClip));
  550. }
  551. }
  552. }
  553. }