190 lines
5.2 KiB
C#
190 lines
5.2 KiB
C#
using NAudio.Wave;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using UnityEngine;
|
|
using UnityEngine.Networking;
|
|
|
|
public class MediaPlayer : MonoBehaviour
|
|
{
|
|
public static MediaPlayer Instance;
|
|
public AudioSource audio_source;
|
|
|
|
private void Awake()
|
|
{
|
|
Instance = this;
|
|
}
|
|
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
audio_source = UIScene.Instance.audio_source;
|
|
}
|
|
|
|
public string media;
|
|
private void Update()
|
|
{
|
|
if (play_audio_while_loaded && !audio_loading)
|
|
{
|
|
play_audio_while_loaded = false;
|
|
audio_source?.Play();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Ô¼ÓÔØÒôƵ
|
|
/// </summary>
|
|
/// <param name="_file_name"></param>
|
|
/// <param name="_callback"></param>
|
|
public void AudioLoad(string _file_name, Action<bool, string> _callback = null)
|
|
{
|
|
if (string.IsNullOrEmpty(_file_name))
|
|
{
|
|
if (_callback != null)
|
|
{
|
|
_callback(false, "ÎÞ´ËÒôƵ:" + _file_name);
|
|
}
|
|
return;
|
|
}
|
|
audio_loading = true;
|
|
var __res = MediaViwerPanel.file_dic.Find(x => x.file_name_text.text.Equals(_file_name));
|
|
var url = "";
|
|
if (null != __res)
|
|
{
|
|
url = __res.file_path;
|
|
}
|
|
else
|
|
{
|
|
if (_callback != null)
|
|
{
|
|
_callback(false, "ÎÞ´ËÒôƵ:" + _file_name);
|
|
}
|
|
return;
|
|
}
|
|
|
|
FileInfo fileInfo = new FileInfo(@url);
|
|
var directoryUrl = Path.Combine(System.Environment.CurrentDirectory + "/Cache");
|
|
var cacheUrl = Path.Combine(System.Environment.CurrentDirectory + "/Cache", fileInfo.Name.Replace(".mp3", ".wav"));
|
|
if (!Directory.Exists(directoryUrl))
|
|
{
|
|
Directory.CreateDirectory(directoryUrl);
|
|
}
|
|
|
|
if (!File.Exists(@cacheUrl))
|
|
{
|
|
using (var audioStrem = new Mp3FileReader(@url))
|
|
{
|
|
WaveFileWriter.CreateWaveFile(@cacheUrl, audioStrem);
|
|
}
|
|
}
|
|
|
|
StartCoroutine(LoadAudio(@cacheUrl, fileInfo.Name, _callback));
|
|
}
|
|
|
|
/// <summary>
|
|
/// мÓÔØÒôƵ
|
|
/// </summary>
|
|
/// <param name="_file_name"></param>
|
|
/// <param name="_callback"></param>
|
|
public void NewAudioLoad(string _file_name, Action<bool, string> _callback = null)
|
|
{
|
|
ConsolePanel.ConsoleOutput("ÕýÔÚ¼ÓÔØÓïÒô", "log");
|
|
if (File.Exists(_file_name))
|
|
{
|
|
//Åжϸñʽ
|
|
string ex = Path.GetExtension(_file_name);
|
|
if(ex==".mp3")
|
|
{
|
|
var directoryUrl = Path.Combine(System.Environment.CurrentDirectory + "/Cache");
|
|
var cacheUrl = Path.Combine(System.Environment.CurrentDirectory + "/Cache", Path.GetFileNameWithoutExtension(_file_name)+ ".wav");
|
|
if (!Directory.Exists(directoryUrl))
|
|
{
|
|
Directory.CreateDirectory(directoryUrl);
|
|
}
|
|
|
|
if (!File.Exists(@cacheUrl))
|
|
{
|
|
using (var audioStrem = new Mp3FileReader(_file_name))
|
|
{
|
|
WaveFileWriter.CreateWaveFile(@cacheUrl, audioStrem);
|
|
}
|
|
}
|
|
|
|
StartCoroutine(LoadAudio(@cacheUrl,Path.GetFileName(cacheUrl) , _callback));
|
|
}
|
|
else if(ex == ".wav")
|
|
{
|
|
StartCoroutine(LoadAudio(_file_name, Path.GetFileName(_file_name), _callback));
|
|
}
|
|
else
|
|
{
|
|
if (_callback != null)
|
|
{
|
|
_callback(false, "ÒôƵ¸ñʽ²»Ö§³Ö:" + ex);
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (_callback != null)
|
|
{
|
|
_callback(false, "ÎÞ´ËÒôƵ:" + _file_name);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
bool audio_loading;
|
|
/// <summary>
|
|
/// ¼ÓÔØÒôƵ
|
|
/// </summary>
|
|
/// <param name="_url"></param>
|
|
/// <returns></returns>
|
|
private IEnumerator LoadAudio(string _url, string _file_name, Action<bool, string> _callback = null)
|
|
{
|
|
audio_loading = true;
|
|
using (UnityWebRequest request = UnityWebRequestMultimedia.GetAudioClip(@_url, AudioType.WAV))
|
|
{
|
|
yield return request.SendWebRequest();
|
|
|
|
var audio_clip = DownloadHandlerAudioClip.GetContent(request);
|
|
if (audio_clip != null)
|
|
audio_source.clip = audio_clip;
|
|
audio_loading = false;
|
|
_callback?.Invoke(true, Path.GetFileNameWithoutExtension(_file_name));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// ¼ÓÔØÍê±Ïºó²¥·Å
|
|
/// </summary>
|
|
bool play_audio_while_loaded;
|
|
/// <summary>
|
|
/// ²¥·ÅÒôÀÖ
|
|
/// </summary>
|
|
public void AudioPlay()
|
|
{
|
|
if (audio_source?.clip != null && !audio_loading)
|
|
audio_source?.Play();
|
|
else
|
|
play_audio_while_loaded = true;
|
|
}
|
|
|
|
public void AudioPause()
|
|
{
|
|
audio_source?.Pause();
|
|
}
|
|
|
|
public void AudioStop()
|
|
{
|
|
audio_source?.Stop();
|
|
}
|
|
|
|
public void AudioSetVolume(float _volume)
|
|
{
|
|
if (audio_source)
|
|
audio_source.volume = _volume;
|
|
}
|
|
}
|