コード例 #1
0
        /// <summary>
        /// Stops the playback of the current instance indicating whether the stop should occur immediately of at the end of the sound.
        /// </summary>
        /// <param name="immediate">A value indicating whether the playback should be stopped immediately or at the end of the sound.</param>
        public void Stop(bool immediate)
        {
            if (immediate && IsLooped)
            {
                voice.ExitLoop();
            }

            Stop();
        }
コード例 #2
0
 /// <summary>
 /// Stop audio
 /// </summary>
 public void Stop()
 {
     if (!isPlaying)
     {
         return;
     }
     _voice.ExitLoop();
     _voice.Stop();
     _voice.FlushSourceBuffers();
     isPlaying = false;
     _checkThread.Abort();
 }
コード例 #3
0
        /// <summary>
        /// Stops the playback of the current instance indicating whether the stop should occur immediately of at the end of the sound.
        /// </summary>
        /// <param name="immediate">A value indicating whether the playback should be stopped immediately or at the end of the sound.</param>
        /// <exception cref="ObjectDisposedException">Is thrown if the current instance was already disposed.</exception>
        public void Stop(bool immediate)
        {
            if (IsDisposed)
            {
                throw new ObjectDisposedException(this.GetType().FullName);
            }

            if (immediate)
            {
                voice.Stop(0);
            }
            else if (IsLooped)
            {
                voice.ExitLoop();
            }
            else
            {
                voice.Stop((int)PlayFlags.Tails);
            }

            paused = false;
        }
コード例 #4
0
 internal override void ExitLoopImpl()
 {
     SourceVoice.ExitLoop();
 }
コード例 #5
0
        private void process()
        {
            lock (lockObject)
                preparing = true;
            byte[] outBuffer = new Byte[4096];
            if (byteStream == null)
            {
                oggFile = new OggVorbisFileStream(fileNames[playPointer]);
            }
            else
            {
                oggStream = new OggVorbisEncodedStream(byteStream);
            }


            MemoryStream PcmStream = null;
            int          PcmBytes  = -1;

            PcmStream = new MemoryStream();
            WaveFormat waveFormat = new WaveFormat();

            AudioBuffer[] theBuffers         = new AudioBuffer[maxBuffers];
            int           nextBuffer         = 0;
            bool          firstLoop          = true;
            bool          startedSourceVoice = false;

            // Decode the Ogg Vorbis data into its PCM data
            while (PcmBytes != 0)
            {
                // Get the next chunk of PCM data, pin these so the GC can't
                while (true)
                {
                    PcmBytes = (oggStream == null) ? oggFile.Read(outBuffer, 0, outBuffer.Length)
                                                : oggStream.Read(outBuffer, 0, outBuffer.Length);


                    if (PcmBytes == 0)                     //Reached the end
                    {
                        break;
                    }
                    PcmStream.Flush();
                    PcmStream.Position = 0;
                    PcmStream.Write(outBuffer, 0, PcmBytes);
                    PcmStream.Position = 0;
                    if (theBuffers[nextBuffer] != null)
                    {
                        theBuffers[nextBuffer].Stream.Dispose();
                        theBuffers[nextBuffer] = null;
                    }

                    theBuffers[nextBuffer]            = new AudioBuffer(SharpDX.DataStream.Create <byte>(PcmStream.ToArray(), true, false));
                    theBuffers[nextBuffer].AudioBytes = PcmBytes;
                    theBuffers[nextBuffer].LoopCount  = 0;
                    if (firstLoop)
                    {
                        VorbisInfo info = (oggStream == null) ? oggFile.Info : oggStream.Info;
                        //BlockAlign = info.Channels * (bitsPerSample / 8);
                        //AverageBytesPerSecond = info.Rate * BlockAlign;

                        //waveFormat.AverageBytesPerSecond = AverageBytesPerSecond;
                        //waveFormat.BitsPerSample = (short)bitsPerSample;
                        //waveFormat.BlockAlignment = (short)BlockAlign;
                        //waveFormat.Channels = (short)info.Channels;
                        //waveFormat.SamplesPerSecond = info.Rate;
                        waveFormat = new WaveFormat(info.Rate, bitsPerSample, info.Channels);
                        //waveFormat.Encoding= WaveFormatEncoding.Pcm;

                        sourceVoice = new SourceVoice(device, waveFormat);

                        sourceVoice.SetVolume(volume);
                    }                     //if first time looping, create sourcevoice

                    sourceVoice.SubmitSourceBuffer(theBuffers[nextBuffer], null);
                    if (nextBuffer == theBuffers.Length - 1)
                    {
                        nextBuffer = 0;
                    }
                    else
                    {
                        nextBuffer++;
                    }
                    //If we're done filling the buffer for the first time
                    if (!startedSourceVoice &&
                        sourceVoice.State.BuffersQueued
                        == maxBuffers)
                    {
                        sourceVoice.Start();
                        startedSourceVoice = true;
                        lock (lockObject)
                        {
                            playing   = true;
                            preparing = false;
                        }                         //lock
                    }
                    firstLoop = false;
                    if (startedSourceVoice)
                    {
                        while (sourceVoice.State.BuffersQueued
                               > maxBuffers - 1)
                        {
                            if (stopNow)
                            {
                                break;
                            }
                            Thread.Sleep(5);
                        }
                    }                     //if started source voice
                    if (stopNow)
                    {
                        break;
                    }
                }                //while
                if (stopNow)
                {
                    break;
                }
                //We don't have any more data but file could still be playing the remaining data.
                if (PcmBytes == 0 /*&& !loop*/)
                {
                    if (!stopNow)
                    {
                        while (sourceVoice.State.BuffersQueued > 0 &&
                               !stopNow)
                        {
                            Thread.Sleep(10);
                        }
                    }                     //if doesn't want to stop ogg
                    if (!loop)
                    {
                        break;    //exit the loop since we ran out of data and don't want to loop back
                    }
                }                 //if we ran out of data
                if (PcmBytes == 0 && loop)
                {
                    PcmBytes = -1;
                    if (oggFile != null)
                    {
                        oggFile.Position = 0;
                    }
                    if (oggStream != null)
                    {
                        oggStream.Position = 0;
                    }
                }         //if we ran out of data but want to loop back
            }             //while more data

            //Done playing, or file requested stop,
            //so clean up and tell calling thread that
            //buffer has stopped and cleaned up.
            //calling thread doesn't know buffer has stopped until we clean things up
            //so we don't lose memory

            //Clean up the resources
            if (sourceVoice != null)
            {
                sourceVoice.ExitLoop();                 //stop looping if looping
                sourceVoice.Stop();
            }
            sourceVoice.Dispose();
            sourceVoice = null;
            if (oggFile != null)
            {
                oggFile.Close();
                oggFile = null;
            }
            outBuffer = null;
            for (int i = 0; i < theBuffers.Length; i++)
            {
                if (theBuffers[i] != null)
                {
                    theBuffers[i].Stream.Dispose();
                    theBuffers[i] = null;
                }
            }
            theBuffers = null;
            if (oggStream != null)
            {
                oggStream.Close();
                oggStream = null;
            }
            PcmStream.Dispose();
            PcmStream = null;
            if (stopEvent != null)
            {
                stopEvent();
            }
        }         //method