132 lines
3.3 KiB
C#
132 lines
3.3 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
using Coolape;
|
|
|
|
public class Mp3PlayerByUrl : MonoBehaviour
|
|
{
|
|
public AudioSource audioSource;
|
|
public string mUrl;
|
|
public AudioType mDefaultAudioType = AudioType.MPEG;
|
|
public UnityWebRequest www;
|
|
public AudioClip myClip;
|
|
public object finishCallback;
|
|
public object progressCallback;
|
|
public bool isPlaying = false;
|
|
|
|
public UnityWebRequest getAudioClip(string url, object callback)
|
|
{
|
|
StartCoroutine(_getAudioClip(url, mDefaultAudioType, callback));
|
|
return www;
|
|
}
|
|
|
|
public UnityWebRequest getAudioClip(string url, AudioType audioType , object callback)
|
|
{
|
|
StartCoroutine(_getAudioClip(url, audioType, callback));
|
|
return www;
|
|
}
|
|
|
|
public IEnumerator _getAudioClip(string url, AudioType audioType ,object callback)
|
|
{
|
|
if (string.IsNullOrEmpty(url))
|
|
{
|
|
yield return null;
|
|
}
|
|
www = UnityWebRequestMultimedia.GetAudioClip(url, audioType);
|
|
using (www)
|
|
{
|
|
yield return www.SendWebRequest();
|
|
|
|
if (www.isNetworkError)
|
|
{
|
|
Debug.Log(www.error);
|
|
}
|
|
else
|
|
{
|
|
myClip = DownloadHandlerAudioClip.GetContent(www);
|
|
Utl.doCallback(callback, myClip);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void play(AudioClip clip, object progressCb, object finishCb)
|
|
{
|
|
if (clip == null) return;
|
|
progressCallback = progressCb;
|
|
finishCallback = finishCb;
|
|
audioSource.clip = myClip;
|
|
audioSource.time = 0;
|
|
audioSource.Play();
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
if (audioSource == null || audioSource.clip == null) return;
|
|
if (!audioSource.isPlaying && isPlaying && Mathf.Abs(1 - audioSource.time / audioSource.clip.length) <= 0.01)
|
|
{
|
|
isPlaying = audioSource.isPlaying;
|
|
Utl.doCallback(finishCallback, audioSource.clip);
|
|
//stop();
|
|
audioSource.time = 0;
|
|
}
|
|
else if(audioSource.isPlaying)
|
|
{
|
|
isPlaying = audioSource.isPlaying;
|
|
Utl.doCallback(progressCallback, audioSource.time / audioSource.clip.length, audioSource.time);
|
|
}
|
|
}
|
|
|
|
public float progress()
|
|
{
|
|
if (audioSource == null || audioSource.clip == null)
|
|
{
|
|
return 0;
|
|
}
|
|
return audioSource.time / audioSource.clip.length;
|
|
}
|
|
|
|
public void stop()
|
|
{
|
|
audioSource.Stop();
|
|
}
|
|
|
|
public void release()
|
|
{
|
|
stop();
|
|
if (audioSource.clip != null)
|
|
{
|
|
Destroy(audioSource.clip);
|
|
audioSource.clip = null;
|
|
}
|
|
}
|
|
|
|
public void pause()
|
|
{
|
|
audioSource.Pause();
|
|
}
|
|
|
|
public void rePlay()
|
|
{
|
|
audioSource.UnPause();
|
|
if (!audioSource.isPlaying)
|
|
{
|
|
audioSource.Play();
|
|
}
|
|
}
|
|
|
|
public void seek(float persent)
|
|
{
|
|
if (audioSource != null)
|
|
{
|
|
float sec = audioSource.clip.length * persent;
|
|
audioSource.time = sec;
|
|
if (!audioSource.isPlaying)
|
|
{
|
|
audioSource.Play();
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|