public void PlayAudio1(string audioFile, bool isSend = true) { GlobalUser.WavePlayer?.Stop(); string reueUrl = ""; try { GlobalUser.WavePlayer = CreateWavePlayer(); 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)) { throw new Exception("无效的本地内容文件"); } var sfile = FileSecretHelper.DecryptFile0(pfile); //if (!audioFile.ToLower().Contains(".mp3")) // reueUrl = $"{audioFile}.mp3"; Mp3FileReader mfr = new Mp3FileReader(sfile); //reueUrl = WebApiProxy.GetRedirectUrl($"{WebApiProxy.MEDIAURL}{audioFile}"); //if (!audioFile.ToLower().Contains(".mp3")) // reueUrl = $"{audioFile}.mp3"; //AudioFileReader afr = // new AudioFileReader(Path.Combine(GlobalUser.AUDIODATAFOLDER, Path.GetFileName(audioFile))); GlobalUser.WavePlayer.Init(mfr);//存入 网络音频地址 GlobalUser.WavePlayer.PlaybackStopped += OnPlaybackStopped1; //TotalTime = Convert.ToInt32(mfr.TotalTime.Minutes * 60 + mfr.TotalTime.Seconds); //PlayTime = 0; if (isSend) { SendProgress(); } GlobalUser.WavePlayer.Play(); } catch (Exception) { try { GlobalUser.WavePlayer?.Stop(); GlobalUser.WavePlayer?.Dispose(); GlobalUser.WavePlayer = CreateWavePlayer(); if (audioFile.ToLower().Contains("records.")) { reueUrl = $"http://{audioFile}.mp3"; } else { reueUrl = WebApiProxy.GetRedirectUrl($"{WebApiProxy.MEDIAURL}{audioFile}"); } MediaFoundationReader mfr = new MediaFoundationReader(reueUrl); GlobalUser.WavePlayer.Init(mfr);//存入 网络音频地址 GlobalUser.WavePlayer.PlaybackStopped += OnPlaybackStopped1; TotalTime = Convert.ToInt32(mfr.TotalTime.Minutes * 60 + mfr.TotalTime.Seconds); PlayTime = 0; SendProgress(); GlobalUser.WavePlayer.Play(); } catch (Exception e) { Log4NetHelper.Error( $"Play Audio Error ---- {e.Message} ---- {e.StackTrace} \r\n-------- url: {WebApiProxy.MEDIAURL}{audioFile} \r\n-------- True Url:{reueUrl}"); BeginExam(_NextFlowType); } } }
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)); } }