public void PlayLooping() { if (_buffer != null) { _playFlag = PlayFlags.Looping; _buffer.CurrentPosition = 0; _buffer.Play(0, _playFlag); } }
public bool Play() { if (_buffer != null) { _playFlag = PlayFlags.None; _buffer.Play(0, _playFlag); return(true); } return(false); }
public static extern bool PlaySound(string lpszName, int hModule, PlayFlags dwFlags);
private void process(bool initializeNextTrack) { preparing = true; SecondarySoundBuffer sBuff = null; int p = playPointer; if (initializeNextTrack) { p++; // Point to the next track which we will initialize. } MemoryStream PcmStream = null; PlayFlags f = PlayFlags.None; if (p > soundBuffers.Count - 1) { SoundBufferDescription desc = new SoundBufferDescription(); desc.Flags = BufferFlags.ControlPositionNotify | BufferFlags.ControlVolume | BufferFlags.GetCurrentPosition2 | BufferFlags.GlobalFocus; byte[] outBuffer = new Byte[4096]; oggFile = new OggVorbisFileStream(fileNames[p]); PcmStream = new MemoryStream(); int PcmBytes = -1; WaveFormat waveFormat = new WaveFormat(); // Decode the Ogg Vorbis data into its PCM data while (PcmBytes != 0) { PcmBytes = oggFile.Read(outBuffer, 0, outBuffer.Length); PcmStream.Write(outBuffer, 0, PcmBytes); } VorbisInfo info = oggFile.Info; waveFormat = new WaveFormat(info.Rate, bitsPerSample, info.Channels); desc.Format = waveFormat; desc.BufferBytes = (int)PcmStream.Length; lock (lockObject) // So we don't lose a simultaneous volume change. soundBuffers.Add(sBuff = new SecondarySoundBuffer(device, desc)); sBuff.Write(PcmStream.ToArray(), 0, LockFlags.EntireBuffer); // In a multi-wave playback, only loop the last track. The preceeding tracks are intros. // Next, if we have a multi-file situation, we need to wait for the current file to stop playing before starting the next one. // This handler will also fire when a sound is done playing by default so we can explicitly dispose of the soundBuffer. stoppedSignal = new AutoResetEvent(false); NotificationPosition[] n = { new NotificationPosition() { Offset = (int)PcmStream.Length - 1, WaitHandle = new AutoResetEvent(false) } }; stoppedSignal = (AutoResetEvent)(n[0].WaitHandle); sBuff.SetNotificationPositions(n); } else // If this buffer has already been initialized ahead of time { sBuff = soundBuffers[p]; } if (!initializeNextTrack) { Thread t = new Thread(stopEventHandler); t.Start(); sBuff.Volume = m_volume; f = (loop && p == fileNames.Length - 1) ? PlayFlags.Looping : PlayFlags.None; sBuff.Play(0, f); } if (PcmStream != null) { oggFile.Close(); oggFile.Dispose(); PcmStream.Close(); PcmStream.Dispose(); } if (!initializeNextTrack && playPointer < fileNames.Length - 1) // Prepare the next track. { process(true); } preparing = false; }