예제 #1
0
        protected void StreamMp3(object state)
        {
            sourceStream = FileFromMemoryStream.Create((string)state);

            Thread.Sleep(300);
            if (sourceStream.FilePath.EndsWith(".mp3"))
            {
                try
                {
                    Thread.Sleep(1000);
                    //((FileFromMemoryStream)sourceStream).WaitForRead = true;
                    StartPlaying(new MediaFoundationReader(sourceStream.FilePath));// (Stream)sourceStream));
                }
                catch (Exception)
                {
                    //((FileFromMemoryStream)sourceStream).WaitForRead = false;
                    //sourceStream.Position = 0;
                    //StartPlaying(new StreamMediaFoundationReader((Stream)sourceStream));
                }
            }
            else if (sourceStream.FilePath.EndsWith(".wav"))
            {
                try
                {
                    StartPlaying(new WaveFileReader((Stream)sourceStream));
                }
                catch (EndOfStreamException)
                {
                }
            }
            else
            {
                throw new Exception("Unsupported file type.");
            }
        }
예제 #2
0
        /// <summary>
        /// Returns a new <see cref="WaveFormat"/> depending on the file in the memory.
        /// </summary>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"></exception>
        public virtual WaveFormat GetWaveFormat(IFileFromMemoryStream sourceStream)
        {
            WaveFormat    format;
            Mp3Frame      frame    = null;;
            AudioFileType fileType = sourceStream.FilePath.EndsWith(".mp3") ?
                                     AudioFileType.Mp3 :
                                     sourceStream.FilePath.EndsWith(".wav") ?
                                     AudioFileType.Wave :
                                     AudioFileType.Unknown;

            if (fileType == AudioFileType.Mp3)
            {
                int counter = 0;
                while (frame == null)
                {
                    frame = Mp3Frame.LoadFromStream((Stream)sourceStream);
                    Thread.Sleep(50);
                    counter++;
                    if (counter > 100)
                    {
                        throw new ArgumentNullException();
                    }
                }

                format = new Mp3WaveFormat(frame.SampleRate, frame.ChannelMode == ChannelMode.Mono ? 1 : 2,
                                           frame.FrameLength, frame.BitRate);
            }
            else if (fileType == AudioFileType.Wave)
            {
                format = (new WaveFileReader(sourceStream.FilePath)).WaveFormat;
            }
            else
            {
                throw new ArgumentException("The file type is currently unsupported");
            }

            return(format);
        }