How to save byte[] as wav file in silverlight application?

試著忘記壹切 提交于 2019-12-11 08:33:50

问题


In my MVC Application I added a Silverlight Audio Recorder. This recorder record audio using System.Windows.Media; namespace as byte[] that represents an wav file.

Now I need to save this byte[] as .wav file on local disk (server).

So far I've tried the following:

byte[] audioData; // Here I contain my data     

System.IO.File.WriteAllBytes(@"C:\Temp\yourfile.wav", this.audioData); // Doesn't work

BinaryWriter Writer = null;
string name = @"C:\Temp\yourfile.wav";

try
{
      Writer = new BinaryWriter(File.OpenWrite(name));

      Writer.Write(this.audioData);
      Writer.Flush();
      Writer.Close();
 }
 catch
 {
      ...
 }

But nothing work for me... What I did wrong?

Ok. Let's say I can send my data as json string to the controller:

WebClient net = new WebClient();
string data = UTF8Encoding.UTF8.GetString(this.audioData, 0, this.audioData.Length);

net.UploadStringAsync(new Uri("/Home/SaveFile", UriKind.Relative), data);

How would I get this data in MVC Action?


回答1:


Silverlight is running client side, your MVC code is server-side, so this won't work in it's current guise.

I'd recommend using a web service to send the data back to your server to save it.

Take a look at using WCF or maybe WebAPI to get up and running with it.

If you really wanted to do get intricate, you could also use something like SignalR to have the client / server (which is essentially what this set up is) send communicate with each other and send the data back through that. Though it seems a little overkill for this.



来源:https://stackoverflow.com/questions/21240586/how-to-save-byte-as-wav-file-in-silverlight-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!