using NAudio.Wave; using System; using System.Collections.Generic; using System.Linq; using System.Reflection; using System.Resources; using System.Text; using System.Threading.Tasks; using CDSAE3_Lian_Lian_Kan.Properties; using NAudio.Wave.SampleProviders; namespace CDSAE3_Lian_Lian_Kan.Sound { public class Song_Audio_processer:IDisposable { //class Audio_File_Processor //{ // private Wave16ToFloatProvider? wave16ToFloatProvider; // private List audioFiles = new List(); // int next_song = 0; // int volume = 90; // internal void set_Albums(string s)=>audioFiles = Settings.musics.TryGetValue(s, out List? val) ? val : new List(); // internal Wave16ToFloatProvider get_next_song() // { // string name = audioFiles[next_song]; // next_song++; // next_song %= audioFiles.Count; // name = name.Split('.').First().Replace(" ","_").Replace("-","_"); // object obj = Settings.res_Manager.GetObject(name, Settings.res_Culture)!; // MemoryStream sound = new MemoryStream((byte[])obj); // MemoryStream ms = new MemoryStream(StreamToBytes(sound)); // var ws = new Mp3FileReader(ms); // BlockAlignReductionStream blockAlignReductionStream = new BlockAlignReductionStream(ws); // wave16ToFloatProvider = new Wave16ToFloatProvider(blockAlignReductionStream); // wave16ToFloatProvider.Volume = volume / 100f; // return wave16ToFloatProvider; // } // internal Wave16ToFloatProvider get_last_song() // { // next_song = (next_song - 2 + audioFiles.Count) % audioFiles.Count; // return get_next_song(); // } // internal void volume_change(int val) // { // volume = val; // if(wave16ToFloatProvider != null) // wave16ToFloatProvider.Volume = volume / 100f; // } // internal static byte[] StreamToBytes(Stream stream) // { // long originalPosition = 0; // if (stream.CanSeek) // { // originalPosition = stream.Position; // stream.Position = 0; // } // try // { // byte[] readBuffer = new byte[4096]; // int totalBytesRead = 0; // int bytesRead; // while ((bytesRead = stream.Read(readBuffer, totalBytesRead, readBuffer.Length - totalBytesRead)) > 0) // { // totalBytesRead += bytesRead; // if (totalBytesRead == readBuffer.Length) // { // int nextByte = stream.ReadByte(); // if (nextByte != -1) // { // byte[] temp = new byte[readBuffer.Length * 2]; // Buffer.BlockCopy(readBuffer, 0, temp, 0, readBuffer.Length); // Buffer.SetByte(temp, totalBytesRead, (byte)nextByte); // readBuffer = temp; // totalBytesRead++; // } // } // } // byte[] buffer = readBuffer; // if (readBuffer.Length != totalBytesRead) // { // buffer = new byte[totalBytesRead]; // Buffer.BlockCopy(readBuffer, 0, buffer, 0, totalBytesRead); // } // return buffer; // } // finally // { // if (stream.CanSeek) // { // stream.Position = originalPosition; // } // } // } //} AudioPlayer? audioPlayer; private List audioFiles = new List(); int next_song = 1; private void OnPlaybackStopped(string s,object? obj) { set_song(get_next_song()); } public void pause_song()=> audioPlayer?.pause_song(); public void resume_song()=> audioPlayer?.resume_song(); public string get_next_song() { string name = audioFiles[next_song]; next_song++; next_song %= audioFiles.Count; return name; } /// /// 调整音乐音量 /// /// 音量大小 1-100 public void volume_change(int val)=> audioPlayer?.volume_change(val); public void set_albums(string s) { var result = Etcs.musics.Where(x => x.Key == s).ToList(); if (result.Count == 0) throw new Exception("no such album"); audioFiles = result.First().Value; } public void set_song(string s) { audioPlayer?.Dispose(); try { audioPlayer = new AudioPlayer(s, Etcs.Song_Volume, OnPlaybackStopped); } catch (Exception e) { MessageBox.Show($"failed to play {s}\n{e.Message}"); return; } audioPlayer.resume_song(); } public void Dispose()=> audioPlayer?.Dispose(); ~Song_Audio_processer() => Dispose(); } }