private void Download(object obj) { try { Log4NetHelper.Info(GlobalUser.AUDIODATAFOLDER); string url = obj as string; if (!url.Contains("records.")) { url = $"{WebApiProxy.MEDIAURL}{url}"; } var fileByte = WebApiProxy.GetAudioFile(url); if (fileByte != null) { File.WriteAllBytes(Path.Combine(GlobalUser.AUDIODATAFOLDER, Path.GetFileName(url)), fileByte); } else { Log4NetHelper.Error($"下载 音频失败, err:{url}"); } } catch (Exception ex) { Log4NetHelper.Error($"下载 音频失败, err:{ex}"); } }
private void PlayAudio(string audioFile) { if (GlobalUser.WavePlayer?.PlaybackState == PlaybackState.Playing) { CleanUp(); PlayIconKind.Kind = PackIconKind.Play; PlayTime = 0; return; } try { GlobalUser.WavePlayer = CreateWavePlayer(); if (!audioFile.Contains("http://")) { audioFile = $"http://{audioFile}"; } try { Stream sfile; string pfile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, GlobalUser.DATAFOLDER, SecurityHelper .HmacMd5Encrypt(GlobalUser.SelectPaperName + GlobalUser.SelectPaperNumber, GlobalUser.FILEPWD, Encoding.UTF8).ToUpper(), SecurityHelper.HmacMd5Encrypt(Path.GetFileNameWithoutExtension(audioFile), GlobalUser.FILEPWD, Encoding.UTF8).ToLower() + ".qf"); if (!File.Exists(pfile)) { if (Environment.OSVersion.Version.Major < 6) { //Vista 以下操作系统执行 //获取流文件 byte[] audioByte = WebApiProxy.GetAudioFile($"{audioFile}.mp3"); sfile = new MemoryStream(audioByte); Mp3FileReader mfr0 = new Mp3FileReader(sfile); GlobalUser.WavePlayer = CreateWavePlayer(); SliderTimer.Maximum = TotalTime = Convert.ToInt32(mfr0.TotalTime.TotalSeconds); TxtPlayTime.Text = TotalTime.ToString(); GlobalUser.WavePlayer.Init(mfr0); GlobalUser.WavePlayer.PlaybackStopped += OnPlaybackStopped; SliderTimer.Value = PlayTime = 0; _dTimer.Start(); PlayIconKind.Kind = PackIconKind.Stop; GlobalUser.WavePlayer.Play(); return; } throw new Exception("无效的本地内容文件"); } sfile = FileSecretHelper.DecryptFile0(pfile); //if (!audioFile.ToLower().Contains(".mp3")) // reueUrl = $"{audioFile}.mp3"; Mp3FileReader mfr = new Mp3FileReader(sfile); //var reader = //new MediaFoundationReader($"{audioFile}.mp3"); // new AudioFileReader(Path.Combine(GlobalUser.AUDIODATAFOLDER, $"{Path.GetFileName(audioFile)}.mp3")); SliderTimer.Maximum = TotalTime = Convert.ToInt32(mfr.TotalTime.TotalSeconds); TxtPlayTime.Text = TotalTime.ToString(); GlobalUser.WavePlayer.Init(mfr); } catch (Exception ex) { try { var reader = new MediaFoundationReader($"{audioFile}.mp3"); SliderTimer.Maximum = TotalTime = Convert.ToInt32(reader.TotalTime.TotalSeconds); TxtPlayTime.Text = TotalTime.ToString(); GlobalUser.WavePlayer.Init(reader); } catch (Exception) { var oggReader = new VorbisWaveReader($"{audioFile}.ogg"); SliderTimer.Maximum = TotalTime = Convert.ToInt32(oggReader.TotalTime.TotalSeconds); TxtPlayTime.Text = TotalTime.ToString(); GlobalUser.WavePlayer.Init(oggReader); } } GlobalUser.WavePlayer.PlaybackStopped += OnPlaybackStopped; SliderTimer.Value = PlayTime = 0; _dTimer.Start(); PlayIconKind.Kind = PackIconKind.Stop; GlobalUser.WavePlayer.Play(); } catch (Exception e) { Log4NetHelper.Error(String.Format("Play Error {0}", e.Message)); } }