quangbd1904 / wami-recorder

Automatically exported from code.google.com/p/wami-recorder
0 stars 0 forks source link

Servidor em ASP.NET - C# #41

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Olá a todos.
Preciso captar o áudio do lado do servidor com uma página ASPX, utilizando 
C#, poderem não estou obtendo sucesso.

Já conseguir fazer alguma coisa, mas o PROBLEMA é que parece que o arquivo 
"wav" fica corrompido e não consigo executa-ló.

Segue o cod abaixo. Alguém poderia me ajudar?

      private static  string caminho="/App_Data/recordings";
        private static  string nomeArquivo="audio-"+Guid.NewGuid()+".wav";

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        public void ProcessRequest(HttpContext context)
        {
            string mapPath=HttpContext.Current.Server.MapPath(caminho);
            string action=context.Request.QueryString["action"];

            if (action.Equals("save"))
            {
                StreamReader inputStream=new StreamReader(context.Request.InputStream);
                string audio=inputStream.ReadToEnd();
                this.SalvaNoArquivo(nomeArquivo, audio);
            }

            if (action.Equals("play"))
            {

                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearContent();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Buffer=true;
                HttpContext.Current.Response.BufferOutput=true;
                HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename="+nomeArquivo);
                HttpContext.Current.Response.Charset="utf-8";
                HttpContext.Current.Response.AddHeader("Content-Type", "audio/x-wav");
                HttpContext.Current.Response.ContentType="audio/x-wav";
                HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
                context.Response.WriteFile(mapPath+"/"+nomeArquivo);
                HttpContext.Current.Response.Expires=-1;
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
        }

        public void SalvaNoArquivo(string fileName, string content)
        {
            string caminhoDoArquivo=Path.Combine(Server.MapPath(caminho), fileName);
            string pasta =HttpContext.Current.Server.MapPath(caminho);

            if (Directory.Exists(pasta)==false)
            {
                Directory.CreateDirectory(pasta);
            }

            FileStream fs=new FileStream(caminhoDoArquivo, FileMode.Create);

            BinaryWriter w=new BinaryWriter(fs);
            try
            {
                w.Write(content);
            }
            finally
            {
                fs.Close();
                w.Close();
            }
        }
    }

Original issue reported on code.google.com by deyvid...@gmail.com on 24 Oct 2013 at 4:33