public void UnPause() { if (State == BufferPlayState.Paused) { buffer.Play(0, PlayFlags.Looping); State = BufferPlayState.Playing; } }
/// <summary> /// The DataTransferThread's work function. Returns, and thread ends, /// when wave data transfer has ended and buffer has played out, or /// when Stop event does EndDataTransferThread(). /// </summary> private void DataTransferActivity() { // load all sectors for (int i = 0; i < NumberOfSectorsInBuffer - 1; i++) { // get a block of bytes, possibly including zero-fill TransferBlockToSecondaryBuffer(); } buffer.CurrentPlayPosition = 0; this.UpdateVolume(); buffer.Play(0, PlayFlags.Looping); State = BufferPlayState.Playing; int endWaveSector = 0; while (MoreWaveDataAvailable) { if (AbortDataTransfer) { return; } //wait here for a notification event NotificationEvent.WaitOne(Timeout.Infinite, true); endWaveSector = SecondaryBufferWritePosition / SectorSize; MoreWaveDataAvailable = TransferBlockToSecondaryBuffer(); } // Fill one more sector with silence, to avoid playing old data during the // time between end-event-notification and SecondaryBuffer.Stop(). Array.Clear(TransferBuffer, 0, TransferBuffer.Length); NotificationEvent.WaitOne(Timeout.Infinite, true); int silentSector; silentSector = SecondaryBufferWritePosition / SectorSize; WriteBlockToSecondaryBuffer(); // No more blocks to write: Remove fill-notify points, and mark end of data. int dataEndInBuffer = DataBytesSoFar % StreamBufferSize; SetEndNotification(dataEndInBuffer); bool notificationWithinEndSectors = false; // end of data or the silent sector // Wait for play to reach the end while (!notificationWithinEndSectors) { NotificationEvent.WaitOne(Timeout.Infinite, true); int currentPlayPos = buffer.CurrentPlayPosition; int currentPlaySector = currentPlayPos / SectorSize; notificationWithinEndSectors = currentPlaySector == endWaveSector | currentPlaySector == silentSector; } buffer.Stop(); State = BufferPlayState.Complete; }
public void Stop() { Burntime.Platform.Log.Debug("stop " + Filename); //UnPause(); EndDataTransferThread(); buffer.Stop(); State = BufferPlayState.Idle; return; }
/// <summary> /// Pause playing the sound file from Playing state, or resume playing from Paused state. /// If state is not Playing nor Paused, has no effect. /// </summary> /// <returns>Buffer.PlayPosition at time of call [bytes]</returns> public int Pause() { int playPosition = buffer.CurrentPlayPosition; if (State == BufferPlayState.Playing) { buffer.Stop(); State = BufferPlayState.Paused; } //else if ( State == BufferPlayState.Paused ) //{ // buffer.Play( 0, PlayFlags.Looping ); // State = BufferPlayState.Playing; //} return(playPosition); }
/// <summary> /// Initializes the soundbuffers /// </summary> /// <param name="FileName">The filename from which to pull the data to initialize the /// soundbuffers</param> public MusicPlayer(Burntime.Platform.IO.File file) { this.Filename = file.Name; Burntime.Platform.Log.Debug("prepare to play " + Filename); oggStream = new OggVorbisFileStream(file.Stream); FullLength = (int)oggStream.Length; format = new WaveFormat(); format.FormatTag = WaveFormatTag.Pcm; format.SamplesPerSecond = oggStream.Info.Rate; format.BitsPerSample = 16; format.Channels = (short)oggStream.Info.Channels; format.BlockAlignment = (short)(format.Channels * (format.BitsPerSample / 8)); format.AverageBytesPerSecond = format.SamplesPerSecond * format.BlockAlignment; SoundBufferDescription description = new SoundBufferDescription(); description.Format = format; description.SizeInBytes = StreamBufferSize; //if ( GameForm.Instance.GameSettings.ForceSoftwareMusicMixing ) description.Flags = BufferFlags.ControlVolume | BufferFlags.GlobalFocus | BufferFlags.ControlPositionNotify | BufferFlags.GetCurrentPosition2 | BufferFlags.Software; //Create the buffer. buffer = new SecondarySoundBuffer(DirectSoundWrapper.Device, description); SecondaryBufferWritePosition = 0; this.UpdateVolume(); // Create a notification Event object, to fire at each notify position NotificationEvent = new AutoResetEvent(false); // Preset as much of the EndNotificationPosition array as possible to avoid doing it // in real-time. EndNotificationPosition[0].Event = NotificationEvent; PredictedEndIndex = (int)(oggStream.Length % StreamBufferSize); //[bytes] // ready to go: MoreWaveDataAvailable = true; State = BufferPlayState.Idle; Play(); }