如何将字符串数据:audio / ogg; base64转换为C#中的文件wav

时间:2014-03-13 10:28:03

标签: c# javascript html5 audio

我有一个这样的字符串:

"data:audio/ogg;base64,T2dnUwACAAAAAAAAAAAbYwAAAAAAABodbEQBE09wdXNIZWFkAQE4AYA+AAAAAABPZ2dTAAAAAAAAAAAAABtjAAABAAAASJutcAE4T3B1c1RhZ3...h3KrMzThXiwEUg="

我使用以下代码通过AJAX帖子获得此字符串:

var reader = new window.FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
    var base64Data = reader.result;
    $.ajax({
        url: "/Home/ConvertToWav",
        type: "POST",
        data: { base64Data : base64Data }
    });
};

我想在行动ConvertToWav中通过C#将此字符串转换为文件wav。

1 个答案:

答案 0 :(得分:2)

尝试使用Alvas.Audio

byte[] byteAry= Convert.FromBase64String(myBase64String);
MemoryStream stream = new MemoryStream(byteAry);
WaveReader wr = new WaveReader(stream);
IntPtr format = wr.ReadFormat();
byte[] data = wr.ReadData();
WaveWriter ww = new WaveWriter(File.Create(fileName + ".wav"),AudioCompressionManager.FormatBytes(format));
ww.WriteData(data);
ww.Close();