public static void Stop() { if (Music.m_XAudio2 == null || Music.Stopped) { return; } Music.m_PlayPosition = TimeSpan.Zero; Music.m_NextPlayPosition = TimeSpan.Zero; Music.m_PlayPositionStart = TimeSpan.Zero; Music.m_Clock.Stop(); ++Music.m_PlayCounter; Music.m_State = (PMediaPlayerState)1; Music.m_PlayEvent.Reset(); if (Music.m_FileStream != null) { ((Stream)Music.m_FileStream).Close(); ((Stream)Music.m_FileStream).Dispose(); Music.m_FileStream = (NativeFileStream)null; } if (Music.m_SourceVoice != null) { Music.m_SourceVoice.Stop(); Music.m_SourceVoice.FlushSourceBuffers(); } if (Music.m_AudioBuffers == null) { return; } for (int index = 0; index < Music.m_AudioBuffers.Length; ++index) { Utilities.FreeMemory((IntPtr)Music.m_MemoryBuffers[index].Pointer); Music.m_MemoryBuffers[index].Pointer = IntPtr.Zero; } }
public static void Play(string fileName) { if (!Music.Stopped && Music.m_FileName == fileName) { return; } if (Music.m_XAudio2 == null) { MediaFactory.Startup(131184, 1); Music.m_XAudio2 = new SharpDX.XAudio2.XAudio2(); Music.m_XAudio2.StartEngine(); Music.m_MasteringVoice = new MasteringVoice(Music.m_XAudio2, 0, 0, 0); Music.m_AudioDecoder = new AudioDecoder(); Music.m_PlayingTask = Task.Factory.StartNew(new System.Action(Music.PlayAsync), TaskCreationOptions.LongRunning); } string path = Engine.FileManager.ResolveMUL(string.Format("music/{0}", (object)fileName)); if (!File.Exists(path)) { return; } if (!Music.Stopped) { Music.Stop(); } Music.m_FileName = fileName; Music.m_FileStream = new NativeFileStream(path, (NativeFileMode)3, 0, (NativeFileShare)1); Music.m_AudioDecoder.SetSourceStream((Stream)Music.m_FileStream); if (Music.m_SourceVoice == null) { Music.m_SourceVoice = new SourceVoice(Music.m_XAudio2, Music.m_AudioDecoder.WaveFormat); Music.m_SourceVoice.BufferEnd += (new System.Action <IntPtr>(Music.SourceVoice_BufferEnd)); } Music.m_SourceVoice.Start(); Music.m_AudioBuffers = new AudioBuffer[3]; Music.m_MemoryBuffers = new DataPointer[Music.m_AudioBuffers.Length]; for (int index = 0; index < Music.m_AudioBuffers.Length; ++index) { Music.m_AudioBuffers[index] = new AudioBuffer(); Music.m_MemoryBuffers[index].Size = 32768; Music.m_MemoryBuffers[index].Pointer = Utilities.AllocateMemory((int)Music.m_MemoryBuffers[index].Size, 16); } PlayUO.Volume volume = Preferences.Current.Music.Volume; if (volume != null && volume.Mute) { return; } if (volume != null) { ((Voice)Music.m_SourceVoice).SetVolume(1f, 0); } bool flag = false; if (Music.Stopped) { ++Music.m_PlayCounter; Music.m_WaitForPlayToOutput.Reset(); flag = true; } else { Music.m_Clock.Start(); } Music.m_State = (PMediaPlayerState)2; Music.m_PlayEvent.Set(); if (!flag) { return; } Music.m_WaitForPlayToOutput.WaitOne(); }