微信录音上传到自己服务器 音频转换后播放的问题

首先我调用了微信接口,录完得音频文件后调用微信的上传录音接口把本地录音先上传到微信的服务器,不过,微信只保留3天,而我们需要长期保存,我们需要把资源从微信服务器下载到自己的服务器,这时调用微信的下载多媒体接口(即官方文档获取临时素材接口)下载到自己服务器, 注意这个接口下载的音频是amr格式,HTML不支持amr格式的音频,需要转成MP3格式的文件。我自己就被这个格式坑了 用audio 播放不了 转成MP3就好了

 

网上有用第三方软件的:ffmpeg ,而我引用的NReco.VideoConverter.dll  这个其实是对ffmpeg 封装的包

1、打开管理NuGet程序包进行安装NReco.VideoConverter.dll

微信录音上传到自己服务器 音频转换后播放的问题

  2、代码  

using NReco.VideoConverter;


/// <summary>
        /// 格式转化
        /// </summary>
        /// <param name="inputFile">源文件路径</param>
        /// <param name="inputFormat">源文件格式</param>
        /// <param name="outFile">转化后文件路径</param>
        /// <param name="outFormat">转化后文件格式</param>
        public static void FormatConversion(string inputFile, string inputFormat, string outFile, string outFormat, int audioSampleRate = 44100)
        {
            try
            {
                new FFMpegConverter().ConvertMedia(inputFile, inputFormat, outFile, outFormat, new ConvertSettings { AudioSampleRate = audioSampleRate });
            }
            catch (Exception ex)
            {
                throw ex;
                // ignored
            }
        }

 

 

 public JsonResult GetMultimedia()
        {
            string file = string.Empty;
            string content = string.Empty;
            string strpath = string.Empty;
            string savepath = string.Empty;
            var appId = WeixinConfig.AppID;
            var appSecret = WeixinConfig.AppSecret;
            var access_token = "";
            using (CommanderServiceClient client = new CommanderServiceClient())
            {
                access_token = client.GetAccessToken(appId, appSecret, false);
            }
            var media_id =Request["serverId"];
            string stUrl = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=" + access_token + "&media_id=" + media_id;

            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(stUrl);

            req.Method = "GET";
            using (WebResponse wr = req.GetResponse())
            {
                HttpWebResponse myResponse = (HttpWebResponse)req.GetResponse();

                strpath = myResponse.ResponseUri.ToString();
                LogWriter.Default.WriteInfo("接收类别://" + myResponse.ContentType);
                WebClient mywebclient = new WebClient();
                savepath = Server.MapPath("/Files/weixinVoice") + "\\" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + (new Random()).Next().ToString().Substring(0, 4) + ".amr";
                LogWriter.Default.WriteInfo("路径://" + savepath);
                try
                {
                    mywebclient.DownloadFile(strpath, savepath);
                    
                    var newfile = savepath.Replace(".amr",".mp3");
                    LogWriter.Default.WriteInfo("新路径://" + newfile);
                    FormatConversion(savepath, "amr", newfile, "mp3");
                    //成功返回文件名称
                    file = System.IO.Path.GetFileName(newfile);
                }
                catch (Exception ex)
                {
                    LogWriter.Default.WriteError("GetMultimedia=>" + ex.Message);
                }

            }
            return Json(file);
        }