public void Stop() { if (isDisplaying != singState.Stop) { audioSource.Stop(); isDisplaying = singState.Stop; } }
public void Pause() { if (isDisplaying == singState.Playing) { audioSource.Pause(); isDisplaying = singState.Pause; } }
private void Update() { if (isDisplaying == singState.Playing) { SuitedTime();//匹配当前时间和歌词 if (audioSource.isPlaying == false) { isDisplaying = singState.Stop; } } }
public bool Play() { //如果没有添加audioClip 或者成功解析LRC 则返回false if (audioSource == null || audioSource.clip == null || timList.Count <= 0 || strList.Count <= 0 || timList.Count != strList.Count) { return(false); } //开始音乐的播放 if (isDisplaying != singState.Playing) { audioSource.Play(); isDisplaying = singState.Playing; } return(false); }
public bool PlaySong(string songPath) { isDisplaying = singState.Stop; //提取音频、和LRC文件的路径 audioPath = songPath; lrcPath = Application.dataPath + "/Resources/" + audioPath.Substring(0, audioPath.LastIndexOf('.') + 1) + "lrc"; //检验音频和LRC是否存在,及可以正常打开 if (!File.Exists(Application.dataPath + "/Resources/" + audioPath) || !File.Exists(lrcPath)) { Debug.Log("Error:file: " + audioPath + " or Lrc not exists!!"); return(false); } try { enc = GetEncoding(lrcPath, Encoding.GetEncoding("UTF-8")); lrcStream = new StreamReader(lrcPath, enc); } catch (Exception e) { Debug.Log("Error: Cant read Lrc File: " + lrcPath + " exception:" + e.Message); return(false); } //解析LRC文件,播放音频 ParseLrcFile(); if (audioSource != null) { audioSource.clip = Resources.Load <AudioClip>(audioPath.Remove(audioPath.LastIndexOf('.')));//"Audio/songs/Adele - Someone Like You" } else { //从自己的component中查找 audioSource = GetComponent <AudioSource>(); //如果自己没有audioSource 则创建一个 if (audioSource == null) { audioSource = gameObject.AddComponent <AudioSource>(); } audioSource.clip = Resources.Load <AudioClip>(audioPath.Remove(audioPath.LastIndexOf('.'))); } audioSource.Play(); isDisplaying = singState.Playing; return(true); }