写音频从文件中的MemoryStream [英] write audio to memorystream from file

查看:162
本文介绍了写音频从文件中的MemoryStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这个文件发送到OutputStream的,但想不通为什么它吐出基本上是空的MP3文件。正如你可以看到我会得到一个异常过早地关闭流,所以我都注释掉了现在。任何指针赞赏。

 使用(的FileStream mp3file = File.OpenRead(NEWFILE))
{
上下文.Response.AddHeader(内容传输编码,二进制);
context.Response.ContentType =音频/ MPEG;
MemoryStream的memStream =新的MemoryStream();
字节[]字节=新的字节[mp3file.Length]
memStream.SetLength(mp3file.Length);
mp3file.Read(memStream.GetBuffer(),0,(int)的mp3file.Length);
memStream.Write(字节,0,(int)的mp3file.Length);
//mp3file.Close();
memStream.WriteTo(context.Response.OutputStream);
//memStream.Close();

}


解决方案

这部分的问题:

 字节[]字节=新的字节[mp3file.Length] 
...
//这里你读入内存流缓冲区...
mp3file.Read(memStream.GetBuffer(),0,(INT)mp3file.Length);
//在这里,你的字节数组全零的覆盖它!
memStream.Write(字节,0,(int)的mp3file.Length);

您不应该的假设的是单个呼叫将实际读到的一切反正虽然。目前尚不清楚你正在使用的.NET版本,但是如果你使用.NET 4.0或更高版本,可以使用的 Stream.CopyTo ,使其更简单。



这是的不清楚为什么您使用的是的MemoryStream 可言。你为什么不只是直接复制到输出流

  mp3File.CopyTo(context.Response.OutputStream)?; 



或者,如果你使用.NET的旧版本:

 字节[]缓冲区=新的字节[16 * 1024]; //对于〔实施例... 
INT读取动作;
而((读取动作= mp3File.Read(缓冲液,0,buffer.Length))大于0)
{
context.Response.OutputStream.Write(缓冲液,0,读取动作);
}



(这是几乎相当于 CopyTo从


I'm trying to send this file to the outputstream but cannot figure out why it spits out basically an empty mp3 file. As you can see I would get an exception closing the stream prematurely so I have commented out for now. Any pointers appreciated.

using (FileStream mp3file = File.OpenRead(newFile))
                {
                    context.Response.AddHeader("content-transfer-encoding", "binary");
                    context.Response.ContentType = "audio/mpeg";
                    MemoryStream memStream = new MemoryStream();
                    byte[] bytes = new byte[mp3file.Length];
                    memStream.SetLength(mp3file.Length);
                    mp3file.Read(memStream.GetBuffer(), 0, (int)mp3file.Length);
                    memStream.Write(bytes, 0, (int)mp3file.Length);
                    //mp3file.Close();
                    memStream.WriteTo(context.Response.OutputStream);
                    //memStream.Close();

                }

解决方案

This part is the problem:

 byte[] bytes = new byte[mp3file.Length];
 ...
 // Here you're reading into the memory stream buffer...
 mp3file.Read(memStream.GetBuffer(), 0, (int)mp3file.Length);
 // And here you're overwriting it with the byte array full of zeroes!
 memStream.Write(bytes, 0, (int)mp3file.Length);

You shouldn't assuming that a single call to Read will actually read everything anyway though. It's not clear which version of .NET you're using, but if you're using .NET 4 or higher, you can use Stream.CopyTo to make it simpler.

It's also unclear why you're using a MemoryStream at all. Why don't you just copy straight to the output stream?

mp3File.CopyTo(context.Response.OutputStream);

Or if you're using an older version of .NET:

byte[] buffer = new byte[16 * 1024]; // For exmaple...
int bytesRead;
while ((bytesRead = mp3File.Read(buffer, 0, buffer.Length)) > 0)
{
    context.Response.OutputStream.Write(buffer, 0, bytesRead);
}

(This is pretty much the equivalent of CopyTo.)

这篇关于写音频从文件中的MemoryStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆