问题描述
我要玩使用NET所提供的机制。由于它与WAV格式,它需要如支持 n音讯库 - 我需要MP3转换为WAV。
I want to play MP3 file downloaded from the web using NET provided System.Media.SoundPlayer mechanism. As it works with WAV formats, it requires the support of e.g. NAudio library - I need to convert MP3 to WAV.
我想要做的所有操作在内存中,因为我需要它来进行所谓的快,但我有问题。下面我已经证明code的正常工作,但它与文件配合。相反,我需要它的工作只使用内存操作。
I want to do all operations in memory, as I need it to be so called fast, but I have problems. Below I've shown code which works as expected, but it cooperates with files. Instead I need to make it working using memory operations only.
(1)工作,但涉及磁盘操作:
(1) works, but involves disk operations:
public void Speak(Uri mp3FileUri)
{
using (var client = new WebClient())
{
using (var networkStream = client.OpenRead(mp3FileUri))
{
if (networkStream != null)
{
var temp = Path.GetTempPath();
var mp3File = Path.Combine(temp, "file.mp3");
var wavFile = Path.Combine(temp, "file.wav");
using (var fileStream = File.Create(mp3File))
{
networkStream.CopyTo(fileStream);
}
using (var reader = new Mp3FileReader(mp3File))
{
WaveFileWriter.CreateWaveFile(wavFile, reader);
}
using(var player = new SoundPlayer(wavFile))
{
player.Play();
}
}
}
}
}
(2)不工作 - 不会抛出异常,但没有播放:
(2) doesn't work - no exception is thrown, but nothing is played:
public void Speak(Uri mp3FileUri)
{
using (var client = new WebClient())
{
using (var networkStream = client.OpenRead(mp3FileUri))
{
if (networkStream != null)
{
var memStream = new MemoryStream();
networkStream.CopyTo(memStream);
memStream.Position = 0;
using (var reader = new Mp3FileReader(memStream))
{
var outStream = new MemoryStream();
using (var writer = new WaveFileWriter(outStream, reader.WaveFormat))
{
var num = 0L;
var buffer = new byte[reader.WaveFormat.AverageBytesPerSecond * 4];
while (true)
{
var count = reader.Read(buffer, 0, buffer.Length);
if (count != 0)
{
num += count;
if (num <= int.MaxValue)
writer.Write(buffer, 0, count);
else
throw new InvalidOperationException("Too large file or endless stream.");
}
else
break;
}
writer.Flush();
outStream.Position = 0;
using(var player = new SoundPlayer(outStream))
{
player.Play(); /* why silence ? */
}
}
}
}
}
}
}
怎样才可以做的,什么是错的第二个code样?
How can it be done and what is wrong with the second code sample ?
推荐答案
根据李哈里森回答,我创建了替代code这是更好的。
Based on Lee Harrison answer, I've created alternative code which is better.
(3)直接播放MP3文件从WEB无需转换为WAV和无磁盘操作的用法:
(3) plays directly MP3 file from WEB without conversion to WAV and without usage of disk operations:
public void Speak(Uri mp3FileUri)
{
using (var client = new WebClient())
{
using (var networkStream = client.OpenRead(mp3FileUri))
{
if (networkStream != null)
{
using (var memStream = new MemoryStream())
{
networkStream.CopyTo(memStream);
memStream.Position = 0;
using (var waveOut = new WaveOut(WaveCallbackInfo.FunctionCallback()))
{
var waveEvent = new ManualResetEvent(false);
waveOut.PlaybackStopped += (sender, e) => waveEvent.Set();
waveEvent.Reset();
using (var rdr = new Mp3FileReader(memStream))
using (var waveStream = WaveFormatConversionStream.CreatePcmStream(rdr))
using (var baStream = new BlockAlignReductionStream(waveStream))
{
waveOut.Init(baStream);
waveOut.Play();
if (waveOut.PlaybackState != PlaybackState.Stopped)
{
waveEvent.WaitOne(); /* block thread for a while because I don't want async play back
(to be analogical as usage of SoundPlayer Play method) */
}
}
}
}
}
}
}
}
不过(尽管这对我来说是相当不错的),我仍然不知道有什么用样品的问题(2)。
Nevertheless (despite this is quite good for me), I still don't know what's the problem with sample (2).
这篇关于用声音播放转换后用n音讯为WAV播放MP3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!