Ejemplo n.º 1
0
        /// <summary>
        /// 开始下载
        /// </summary>
        public void StartDownload(Dictionary <string, string> _aduioFileDic, string savePath,
                                  string mainWin = "MainDialog")
        {
            int d_count = 0;

            string d_url = ""; //下载 路径
            string s_url = ""; //保存路径

            IsDownloading = true;
            DownloadProgressDialogVM downloadProgressDialogVM = new DownloadProgressDialogVM("");
            //显示 等待窗
            var view = new DownloadProgressDialog
            {
                DataContext = downloadProgressDialogVM
            };

            //1.等待窗 (2.执行下载, 3.关闭 等待)
            var result = DialogHostEx.ShowDialog(GlobalUser.MainWin, view, ExtendedOpenedEventHandler,
                                                 ExtendedClosingEventHandler);

            //view.DownloadingSpeedText.Text = "正在下载试题,已完成 100%";

            Application.Current.Dispatcher.Invoke(new Action(() =>
            {
                for (int i = 0; i < _aduioFileDic.Keys.Count; i++)
                {
                    d_url = $"{WebApiProxy.MEDIAURL}{_aduioFileDic[_aduioFileDic.Keys.ToList()[i]]}";
                    s_url = Path.Combine(savePath,
                                         SecurityHelper.HmacMd5Encrypt(Path.GetFileNameWithoutExtension(d_url), GlobalUser.FILEPWD,
                                                                       Encoding.UTF8)
                                         .ToLower() + ".qf");

                    downloadProgressDialogVM.DownloadingCount =
                        Convert.ToInt32(((i + 1) / _aduioFileDic.Keys.Count) * 100);
                    downloadProgressDialogVM.DownloadingCountText = $"总进度 {(i + 1)}/{_aduioFileDic.Keys.Count}";

                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(d_url);
                    //if (DownloadBytes > 0)
                    //{
                    //    request.AddRange(DownloadBytes);
                    //}

                    //发送请求并获取相应回应数据
                    HttpWebResponse response = request.GetResponse() as HttpWebResponse;
                    //var response = request.EndGetResponse(ar);
                    DownloadBytes = TotalBytes = 0;
                    if (this.TotalBytes == 0)
                    {
                        this.TotalBytes = response.ContentLength;
                    }
                    using (var writer = new MemoryStream())
                    {
                        using (var stream = response.GetResponseStream())
                        {
                            while (DownloadBytes != TotalBytes)
                            {
                                byte[] data    = new byte[readBytes];
                                int readNumber = stream.Read(data, 0, data.Length);
                                if (readNumber > 0)
                                {
                                    writer.Write(data, 0, readNumber);
                                    DownloadBytes += readNumber;
                                }

                                int downloadingSpeed = Convert.ToInt32((DownloadBytes * 100 / TotalBytes));
                                downloadProgressDialogVM.DownloadingSpeedText = $"当前 {downloadingSpeed}%";
                                downloadProgressDialogVM.DownloadingSpeed     =
                                    downloadingSpeed == 100.00d ? 99.99d : downloadingSpeed;
                                downloadProgressDialogVM.DownloadingSpeedVisibility =
                                    downloadingSpeed > 1.00f ? Visibility.Visible : Visibility.Hidden;
                            }
                        }

                        if (DownloadBytes == TotalBytes)
                        {
                            // Set the position to the beginning of the stream.
                            writer.Seek(0, SeekOrigin.Begin);

                            FileSecretHelper.EncryptFileBybt(writer, s_url);
                            d_count++;

                            if (d_count == _aduioFileDic.Keys.Count)
                            {
                                Complete();
                            }
                        }
                    }
                }
            }));
        }
Ejemplo n.º 2
0
        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);
                }
            }
        }
Ejemplo n.º 3
0
        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));
            }
        }