예제 #1
0
        private async void TalkButton_Click(object sender, RoutedEventArgs e)
        {
            if (WordsInput.Text.Trim() == "")
            {
                ContentDialog dialog = new ContentDialog()
                {
                    Title           = "您没有输入词语!",
                    CloseButtonText = "去输入"
                };
                await dialog.ShowAsync();

                return;
            }

            TalkArgs args = new TalkArgs();

            args.Words     = this.WordsInput.Text;
            args.SplitChar = ' ';

            if (!int.TryParse(this.Speed.Text, out args.Speed) || args.Speed < 1 || args.Speed > 15)
            {
                args.Speed = 4;
            }

            args.DisableBackward = ((CheckDisableGoback.IsChecked == null
                ? false
                : (bool)(CheckDisableGoback.IsChecked)));
            args.DisableRepeat = ((CheckDisableReplay.IsChecked == null
                ? false
                : (bool)(CheckDisableReplay.IsChecked)));
            args.AutoNext = ((CheckEnableAutoNext.IsChecked == null
                ? false
                : (bool)(CheckEnableAutoNext.IsChecked)));
            args.RandomSort = ((CheckRandomSort.IsChecked == null
                ? false
                : (bool)(CheckRandomSort.IsChecked)));

            if (!int.TryParse(this.TextAutoNextTimes.Text, out args.AutoNextSeconds) || args.Speed < 1)
            {
                args.AutoNextSeconds = 20;
            }

            Frame.Navigate(typeof(TalkPage), args);
        }
예제 #2
0
        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {
            currentTimer = null;
            voices       = null;
            wordsRefer   = "Oops!程序出现了亿些问题";
            voicesIndex  = 0;

            StackWrongs.Children.Clear();
            ButtonOK.Visibility      = Visibility.Collapsed;
            BackButton.IsEnabled     = this.Frame.CanGoBack;
            BlockWords.Text          = "正在初始化......";
            ButtonStop.IsEnabled     =
                ButtonNext.IsEnabled = ButtonPrev.IsEnabled = ButtonRepeat.IsEnabled = false;
            randSort = ((TalkArgs)e.Parameter).RandomSort;
            await ProcessTTSAPI((TalkArgs)e.Parameter);

            BlockWords.Text     = "初始化完成!你准备好了吗?";
            ButtonOK.Visibility = Visibility.Visible;

            args = ((TalkArgs)e.Parameter);
        }
예제 #3
0
        async Task <bool> ProcessTTSAPI(TalkArgs args)
        {
            wordsRefer = args.Words;
            vs         = args.Words.Split(' ');

            if (randSort)
            {
                Random r      = new Random();
                int    rndCnt = r.Next(10, 101);
                for (int k = 0; k != rndCnt; ++k)
                {
                    int a    = r.Next(0, vs.Length),
                        b    = r.Next(0, vs.Length);
                    string t = vs[a];
                    vs[a] = vs[b];
                    vs[b] = t;
                }
            }

            wordsRefer = string.Join(" ", vs);


            //Baidu.Aip.Speech.TtsResponse[] resps = new Baidu.Aip.Speech.TtsResponse[0];
            voices      = new List <string>();
            voicesIndex = 0;

            string tokenJson = await new HttpClient()
                               .GetAsync("https://openapi.baidu.com/oauth/2.0/token?grant_type=client_credentials&client_id=y28tMmG81RCmLFkAfrhM81ql&client_secret=f3nrSGaQSmCHpPql3Deb16pDNaYPWhyv")
                               .Result.Content.ReadAsStringAsync();
            var    tokenRespJson = JObject.Parse(tokenJson);
            string token         = tokenRespJson["access_token"].ToString();

            //BlockWords.Text = token;

            if (Directory.Exists(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "Voices")))
            {
                Directory.Delete(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "Voices"), true);
            }
            Directory.CreateDirectory(
                Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "Voices"));

            int cnt = 0;

            foreach (var i in vs)
            {
                ++cnt;
                BlockWords.Text = "正在生成第" + cnt.ToString() + "个词语...";
                if (i == "")
                {
                    continue;
                }
                var resp = await
                           new HttpClient()
                           .GetAsync("https://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid=DeepDarkFantasy&tok="
                                     + token
                                     + "&tex=" + Uri.EscapeDataString(Uri.EscapeDataString(i))
                                     + "&aue=6"
                                     + "&spd=" + args.Speed.ToString());

                Debug.WriteLine("https://tsn.baidu.com/text2audio?lan=zh&ctp=1&cuid=DeepDarkFantasy&tok="
                                + token
                                + "&tex=" + Uri.EscapeDataString(Uri.EscapeDataString(i))
                                + "&aue=6"
                                + "&spd=" + args.Speed.ToString());

                if (resp.Content.Headers.ContentType.MediaType != "audio/wav")
                {
                    //Debug.Assert(false);
                    BlockWords.Text = "Oops!这个软件出现了亿些问题......";
                    Debug.WriteLine(await resp.Content.ReadAsStringAsync());
                    return(false);
                }
                string wordId = System.Guid.NewGuid().ToString();
                File.WriteAllBytes(
                    Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "Voices", wordId + ".wav"),
                    await resp.Content.ReadAsByteArrayAsync());
                voices.Add(Path.Combine(ApplicationData.Current.TemporaryFolder.Path, "Voices", wordId + ".wav"));

                new System.Threading.ManualResetEvent(false).WaitOne(100);
            }
            return(true);
        }