add
This commit is contained in:
125
Assets/trCRM/Scripts/NAudio/Mp3PlayerByUrl.cs
Normal file
125
Assets/trCRM/Scripts/NAudio/Mp3PlayerByUrl.cs
Normal file
@@ -0,0 +1,125 @@
|
||||
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 audioType = 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, callback));
|
||||
return www;
|
||||
}
|
||||
|
||||
public IEnumerator _getAudioClip(string url, 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/trCRM/Scripts/NAudio/Mp3PlayerByUrl.cs.meta
Normal file
11
Assets/trCRM/Scripts/NAudio/Mp3PlayerByUrl.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 468c5112a721645e78cea88b0e3d1ce7
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user