Tanoda
NAudioPlayer.cs
Go to the documentation of this file.
1using System;
2using System.Collections;
3using System.Collections.Generic;
4using System.IO;
5using NaughtyAttributes;
6using UnityEngine;
7using UnityEngine.Networking;
8
9#if !UNITY_WEBGL
10using NAudio;
11using NAudio.Wave;
12#endif
13public class NAudioPlayer : MonoBehaviour
14{
15 [SerializeField]
16 [ReadOnly]
17 internal string filePath = "";
18 private FileStream fs;
19#if !UNITY_WEBGL
20 private WaveStream fileReader;
21 private WaveOutEvent waveOut;
22#endif
23 public bool isPlaying = false;
24 public event EventHandler SoundStopped;
25
26 protected virtual void OnSoundStopped(EventArgs e)
27 {
28 EventHandler handler = SoundStopped;
29 handler?.Invoke(this, e);
30 }
31
32 public string GetFileName()
33 {
34 return filePath == "" ? "" : filePath.Substring(filePath.LastIndexOf('\\') + 1);
35 }
36
37
38 // Start is called before the first frame update
39 //IEnumerator Start()
40 //{
41 // using (var ms = File.OpenRead(@"C:\Users\UnityTeam\Downloads\file_example_MP3_5MG.mp3"))
42 // using (var mp3Reader = new Mp3FileReader(ms))
43 // using (var waveOut = new WaveOutEvent())
44 // {
45 // waveOut.Init(mp3Reader);
46 // waveOut.Play();
47 // while (waveOut.PlaybackState == PlaybackState.Playing)
48 // {
49 // yield return new WaitForSeconds(0.1f);
50 // }
51 // }
52 //
53 // yield return null;
54 //}
55
56#if UNITY_EDITOR
57 [Button]
58 private void loadtest()
59 {
60 filePath = @"C:\Users\UnityTeam\Downloads\file_example_MP3_5MG.mp3";
61 }
62#endif
63
64 public void SetFilePath(string path)
65 {
66 filePath = path;
67 }
68
69 [Button]
70 public void PlayAudio()
71 {
72#if !UNITY_WEBGL
73 if (filePath == "") return;
74 waveOut?.Dispose();
75 fileReader?.Dispose();
76 fs?.Dispose();
77 fs = File.OpenRead(filePath);
78 if (filePath.ToLower().EndsWith("mp3"))
79 {
80 fileReader = new Mp3FileReader(fs);
81 }
82 else //if (filePath.ToLower().EndsWith("wav"))
83 {
84 fileReader = new WaveFileReader(fs);
85 }
86 waveOut = new WaveOutEvent();
87 waveOut.Init(fileReader);
88 waveOut.Play();
89 waveOut.PlaybackStopped += WaveOut_PlaybackStopped;
90 isPlaying = true;
91#else
92 StartCoroutine(LoadAndPlay());
93#endif
94 }
95
96#if UNITY_WEBGL
97
98 IEnumerator LoadAndPlay()
99 {
100 if (gameObject.GetComponent<AudioSource>())
101 {
102 gameObject.GetComponent<AudioSource>().Play();
103 yield break;
104 }
105 var www = UnityWebRequestMultimedia.GetAudioClip(filePath, AudioType.UNKNOWN);
106 NetworkManager.instance.AuthWebRequest(ref www);
107 www.url = filePath;
108 yield return www.SendWebRequest();
109
110 if (!www.isHttpError && !www.isNetworkError)
111 {
112 var song = gameObject.AddComponent<AudioSource>();
113 song.clip = ((DownloadHandlerAudioClip) www.downloadHandler).audioClip;
114 song.Play();
115 }
116 else
117 {
118 Debug.Log(www.error);
119 }
120
121 }
122#endif
123
124#if !UNITY_WEBGL
125 private void WaveOut_PlaybackStopped(object sender, StoppedEventArgs e)
126 {
128 isPlaying = false;
129 StopAudio();
130 }
131#endif
132 [Button]
133 public void StopAudio()
134 {
135#if !UNITY_WEBGL
136 try
137 {
138 if (waveOut != null)
139 waveOut.PlaybackStopped -= WaveOut_PlaybackStopped;
140 }
141 catch (Exception)
142 {
143 // ignored
144 }
145 waveOut?.Stop();
146 waveOut?.Dispose();
147 fileReader?.Dispose();
148 fs?.Close();
149 fs?.Dispose();
150 isPlaying = false;
151#else
152
153 if (gameObject.GetComponent<AudioSource>())
154 {
155 gameObject.GetComponent<AudioSource>().Stop();
156 }
157#endif
158 }
159
160 private void OnDestroy()
161 {
162#if !UNITY_WEBGL
163 waveOut?.Stop();
164 waveOut?.Dispose();
165 fileReader?.Dispose();
166#endif
167 fs?.Close();
168 fs?.Dispose();
169 }
170}
UnityEngine.UI.Button Button
Definition: Pointer.cs:7
UnityEngine.Debug Debug
Definition: TanodaServer.cs:19
void SetFilePath(string path)
Definition: NAudioPlayer.cs:64
void PlayAudio()
Definition: NAudioPlayer.cs:70
void StopAudio()
EventHandler SoundStopped
Definition: NAudioPlayer.cs:24
virtual void OnSoundStopped(EventArgs e)
Definition: NAudioPlayer.cs:26
string GetFileName()
Definition: NAudioPlayer.cs:32