我设法将wav文件转换为mp3文件。但是当我的应用程序运行时,它只会冻结 VLC 媒体播放器(错误文件描述符)当我关闭我的应用程序时,它会正常播放。
public void ConvertWavToMp3(string filename)
{
LameMP3FileWriter mpFileWriter;
if (File.Exists(Path.Combine(filename.Split('.').FirstOrDefault() + ".mp3"))) File.Delete(Path.Combine(filename.Split('.').FirstOrDefault() + ".mp3"));
using (WaveStream waveStream = WaveFormatConversionStream.CreatePcmStream(new WaveFileReader(filename)))
using (mpFileWriter = new LameMP3FileWriter(File.Create(Path.Combine(filename.Split('.').FirstOrDefault() + ".mp3")), waveStream.WaveFormat, LAMEPreset.ABR_320))
{
byte[] bytes = new byte[waveStream.Length];
waveStream.Position = 0;
waveStream.Read(bytes, 0, (int)waveStream.Length);
mpFileWriter.Write(bytes, 0, bytes.Length);
mpFileWriter.Flush();
}
if (mpFileWriter != null)
{
mpFileWriter.Dispose();
mpFileWriter.Close();
mpFileWriter = null;
}
else Console.WriteLine("file writer was null");
lblstatus.Text = "Ready";
lblstatus.ForeColor = System.Drawing.Color.DarkGreen;
}
如您所见,我关闭并处理了文件,但这不起作用。错误仍然存在。文件本身很好,但应用程序不允许运行或无法播放。奇怪。希望你能帮助我,辛塞勒里,TheSkilluminatiPS:我不是英语母语人士,因此对于文本中的任何问题,我
我不完全确定,但您需要关闭并处理由 File.Create(Path.Combine(filename.Split('.').FirstOrDefault() + ".mp3")
创建的 FileStream
只需在代码中添加另一个using
即可释放 FileStream
string filePath = Path.Combine(filename.Split('.').FirstOrDefault() + ".mp3";
using(FileStream fs = File.Create(filePath))
using (mpFileWriter = new LameMP3FileWriter(fs), waveStream.WaveFormat, LAMEPreset.ABR_320))
{
byte[] bytes = new byte[waveStream.Length];
waveStream.Position = 0;
waveStream.Read(bytes, 0, (int)waveStream.Length);
mpFileWriter.Write(bytes, 0, bytes.Length);
mpFileWriter.Flush();
}
如果这不起作用,我想LameMP3FileWriter
实现对文件处理程序有问题