Example #1
0
        /// <summary>
        /// 构造一个网络音频流
        /// </summary>
        /// <param name="url"></param>
        public NetWorkWaveReader(string url)
        {
            //需要将网络流全部读入内存中,因为网络流是不能直接查找的
            HttpWebRequest request  = (HttpWebRequest)WebRequest.Create(url);
            WebResponse    response = request.GetResponse();

            using (Stream stream = response.GetResponseStream())
            {
                waveStream = new MemoryStream();
                byte[] byteBuffer = new byte[1024];
                if (stream != null)
                {
                    int iReadSize = stream.Read(byteBuffer, 0, byteBuffer.Length);
                    while (iReadSize > 0)
                    {
                        waveStream.Write(byteBuffer, 0, iReadSize);
                        iReadSize = stream.Read(byteBuffer, 0, byteBuffer.Length);
                    }
                    stream.Close();
                    stream.Dispose();
                }
                waveStream.Flush();
                waveStream.Position = 0;
                var chunkReader = new WaveFileChunkReader();
                chunkReader.ReadWaveHeader(waveStream);
                waveFormat      = chunkReader.WaveFormat;
                dataPosition    = chunkReader.DataChunkPosition;
                dataChunkLength = chunkReader.DataChunkLength;
                chunks          = chunkReader.RiffChunks;
                Position        = 0;
                ownInput        = true;
            }
            response.Close();
        }
Example #2
0
        /// <summary>
        /// Creates a Network Wave Reader based on an input stream
        /// </summary>
        /// <param name="inputStream">The input stream containing a WAV data including header</param>
        public NetWorkWaveReader(Stream inputStream)
        {
            waveStream = inputStream;
            var chunkReader = new WaveFileChunkReader();

            chunkReader.ReadWaveHeader(inputStream);
            waveFormat      = chunkReader.WaveFormat;
            dataPosition    = chunkReader.DataChunkPosition;
            dataChunkLength = chunkReader.DataChunkLength;
            chunks          = chunkReader.RiffChunks;
            Position        = 0;
        }