none
wp8.1中语音识别问题? RRS feed

  • 问题

  • 在程序中集成SpeechRecognizer类进行语音识别,设置语言为zh-CN,在模拟器中运行正常,但在真机中运行,报错,catch输出Exception.message为“The text associated with this error code could not be found,The requested laguage is not supported.” 但是设置里面语音识别的语言为中文啊,而且搜索功能里也能识别中文啊 为什么?
    2015年2月8日 1:51

全部回复

  • 你好,

    >>catch输出Exception.message为“The text associated with this error code could not be found,The requested laguage is not supported.” 但是设置里面语音识别的语言为中文啊,而且搜索功能里也能识别中文啊 为什么?

    请问你的真机上区域国家设置是什么,显示的系统语言是什么,还有请提供系统版本号


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年2月9日 11:58
    版主
  • 区域国家:中国

    系统语言:中文(简体)

    系统版本号:8.10.12400.899

    手机型号:Lumia 625(一直无法继续升级,提示空间不足,而我手机是新机什么都没装)

    2015年2月9日 12:39
  • 区域国家:中国

    系统语言:中文(简体)

    系统版本号:8.10.12400.899

    手机型号:Lumia 625(一直无法继续升级,提示空间不足,而我手机是新机什么都没装)


    你好,

    >>设置语言为zh-CN,在模拟器中运行正常,但在真机中运行,报错,catch输出Exception.message

    请问你是如何使用SpeechRecognizer类的,是由定义一些约束吗?还有你的异常是在哪出抛出的,最好单步调试查看。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年2月10日 12:27
    版主
  • private async void Voice_Click(object sender, RoutedEventArgs e)
            {
                SpeechRecognizer speechRecognizers;
                try
                {
                    var mylanguage = (from language in SpeechRecognizer.SupportedGrammarLanguages
                                      where language.LanguageTag == "zh-CN"
                                      select language).FirstOrDefault();
                    speechRecognizers = new SpeechRecognizer(mylanguage);//这句抛出错误
                    SpeechRecognitionCompilationResult compilationResult = await speechRecognizers.CompileConstraintsAsync();
                    if (compilationResult.Status == SpeechRecognitionResultStatus.Success)
                    {
                        var result = await speechRecognizers.RecognizeAsync();
                        if (result.Confidence == SpeechRecognitionConfidence.Rejected)
                        {
                            Output.Text = "语音识别不到";
                        }
                        else
                        {
                            //获取语音识别到的内容
                            Output.Text = result.Text;
                        }
                    }
                }          
                catch (Exception er)
                {
                    new MessageDialog(er.Message).ShowAsync();
                }
            }
    • 已编辑 纸房子 2015年2月10日 13:48 打错了
    2015年2月10日 13:47
  • private async void Voice_Click(object sender, RoutedEventArgs e)
            {
                SpeechRecognizer speechRecognizers;
                try
                {
                    var mylanguage = (from language in SpeechRecognizer.SupportedGrammarLanguages
                                      where language.LanguageTag == "zh-CN"
                                      select language).FirstOrDefault();
                    speechRecognizers = new SpeechRecognizer(mylanguage);//这句抛出错误
                    SpeechRecognitionCompilationResult compilationResult = await speechRecognizers.CompileConstraintsAsync();
                    if (compilationResult.Status == SpeechRecognitionResultStatus.Success)
                    {
                        var result = await speechRecognizers.RecognizeAsync();
                        if (result.Confidence == SpeechRecognitionConfidence.Rejected)
                        {
                            Output.Text = "语音识别不到";
                        }
                        else
                        {
                            //获取语音识别到的内容
                            Output.Text = result.Text;
                        }
                    }
                }          
                catch (Exception er)
                {
                    new MessageDialog(er.Message).ShowAsync();
                }
            }

    你好,

    一般我们可以从设备的 Setting --> Speech --> Speech Language 中查找支持的语音识别的语言,我在自己机器上看是支持中文的。

    我注意到你是在这句代码抛出的异常: speechRecognizers = new SpeechRecognizer(mylanguage);

    我创建了一个DEMO去测试你的代码,发现mylanguage不管是在模拟器还是真机上都是null空值,所以我意识到可能是你的Lambda表达式有问题。

    因为你是按照LanguageTag去作为条件匹配的,我就去遍历SpeechRecognizer.SupportedGrammarLanguages,查看各个item的LanguageTag,发现简体中文是zh-Hans-CN:

    foreach(var lan in SpeechRecognizer.SupportedGrammarLanguages)
    {
           Debug.WriteLine(lan.LanguageTag);
    }


    所以代码应该修改如下:

    var mylanguage = (from language in SpeechRecognizer.SupportedGrammarLanguages
                                      where language.LanguageTag == "zh-Hans-CN"
                                      select language).FirstOrDefault();
    speechRecognizers = new SpeechRecognizer(mylanguage);

    这样可以排除这处问题。


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年2月11日 13:15
    版主
  • 还是不行  error:Exception from HRESULT:0x800455BC
    2015年2月12日 11:17
  • 有别的demo吗,我只想实现一个文本框的语音输入
    2015年2月12日 11:18
  • 还是不行  error:Exception from HRESULT:0x800455BC

    你好,

    >>还是不行  error:Exception from HRESULT:0x800455BC

    请问这个错误是在哪里出现的?


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年2月15日 7:51
    版主
  • 还是那句初始化语句
    2015年2月16日 12:41
  • 还是那句初始化语句

    你好,

    >>还是那句初始化语句

    请问你可以把这段代码输出的结果贴出来吗:

    foreach(var lan in SpeechRecognizer.SupportedGrammarLanguages)
    {
           Debug.WriteLine(lan.LanguageTag);
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年2月17日 11:18
    版主
  • zh-Hans-CN
    en-US
    en-GB
    2015年2月27日 5:32
  • MainPage.xaml.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Navigation;
    using Microsoft.Phone.Controls;
    using Microsoft.Phone.Shell;
    using Voice_C2.Resources;
    using Windows.Phone.Speech.Synthesis;
    using Windows.Phone.Speech.Recognition;
    
    namespace Voice_C2
    {
        public partial class MainPage : PhoneApplicationPage
        {
    
            // Declare the SpeechRecognizer object at the class level.
            SpeechRecognizer myRecognizer;
            SpeechSynthesizer synth;
    
            // 构建MainPage
            public MainPage()
            {
                InitializeComponent();
                voicesComboBox.ItemsSource = new MyLocals().Items();
                // SpeechRecognizer初始化并添加WebSearch grammar.
                myRecognizer = new SpeechRecognizer();
                IEnumerable<SpeechRecognizerInformation> zhRecognizers = from recognizerInfo in InstalledSpeechRecognizers.All
                                                                         where recognizerInfo.Language == "zh-CN"
                                                                         select recognizerInfo;
                myRecognizer.Grammars.AddGrammarFromPredefinedType("citySearch", SpeechPredefinedGrammar.Dictation);
                // 指定 SRGS 语法(可选)
                //Uri mySRGS = new Uri("ms-appx:///SRGSGrammar1.xml", UriKind.Absolute);
                //myRecognizer.Grammars.AddGrammarFromUri("start", mySRGS );
                
                // 初始语言
                displayText.Text = "说点什么让我来听听吧~";
    
                // Subscribe to the AudioCaptureStateChanged event.
                myRecognizer.AudioCaptureStateChanged += myRecognizer_AudioCaptureStateChanged;
            }
    
            // Detect capture state changes and write the capture state to the text block.
            void myRecognizer_AudioCaptureStateChanged(SpeechRecognizer sender, SpeechRecognizerAudioCaptureStateChangedEventArgs args)
            {
                if (args.State == SpeechRecognizerAudioCaptureState.Capturing)
                {
                    this.Dispatcher.BeginInvoke(delegate { displayText.Text = "正在聆听"; });
                    this.Dispatcher.BeginInvoke(delegate { ansText.Text = ""; });
                }
                else if (args.State == SpeechRecognizerAudioCaptureState.Inactive)
                {
                    this.Dispatcher.BeginInvoke(delegate { displayText.Text = "思考中"; });
                }
    
            }
    
            // 按钮单击事件
            private async void Reco1_Click(object sender, RoutedEventArgs e)
            {
    
                // 开始聆听
                SpeechRecognitionResult recoResult = await myRecognizer.RecognizeAsync();
    
                // 检查是否被拒并返回信息
                if (recoResult.TextConfidence == SpeechRecognitionConfidence.Rejected)
                {
                    displayText.Text = "抱歉,我没听清楚, \n重复一下好吗?";
                }
    
                // 检查语音输入是否被recognized with low confidence 并提示用户再说一遍
                else if (recoResult.TextConfidence == SpeechRecognitionConfidence.Low)
                {
                    displayText.Text = "我不太确定你的意思, \n请再说一遍吧。";
                }
    
                // Check to see if speech input was recognized and confirm the result.
                else if (recoResult.TextConfidence == SpeechRecognitionConfidence.High ||
                      recoResult.TextConfidence == SpeechRecognitionConfidence.Medium)
                {
                    displayText.Text = "你刚刚说的是: \n";
                    ansText.Text = recoResult.Text;
                }
                try
                {
                    // Initialize the SpeechSynthesizer object.
                    synth = new SpeechSynthesizer();
    
                    var myLocal = (TTS)voicesComboBox.SelectedItem;
    
                    // Query for a voice. Results rdered by Gender to ensure the order always goes Female then Male.
                    var voices = (from voice in InstalledVoices.All
                                  where voice.Language == myLocal.Lcid
                                  select voice).OrderByDescending(v => v.Gender);
    
                    // gender: 0 = Female, 1 = Male. Corresponds to the index of the above results.
                    int gender = 0;
                    if (MaleRadioButton.IsChecked == true) gender = 1; else gender = 0;
    
                    // Set the voice as identified by the query.
                    synth.SetVoice(voices.ElementAt(gender));
    
                    // Speak
                    await synth.SpeakTextAsync(displayText.Text);
                    await synth.SpeakTextAsync(ansText.Text);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
    
        }
    }

    TTS.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
      
    namespace Voice_C2
    {
        class TTS
        {
            public TTS(string name, string lcid)
            {
                _name = name;
                _lcid = lcid;
            }
      
            private string _name;
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
      
            private string _lcid;
            public string Lcid
            {
                get { return _lcid; }
                set { _lcid = value; }
            }
        }
      
        class MyLocals
        {
            private IList<TTS> _myLocals;
      
            public MyLocals()
            {
                _myLocals = new List<TTS>();
      
                _myLocals.Add(new TTS("Chinese Simplified (PRC)", "zh-CN"));
                _myLocals.Add(new TTS("Chinese Traditional (Taiwan)", "zh-TW"));
                _myLocals.Add(new TTS("English (United States)", "en-US"));
                //_myLocals.Add(new TTS("English (United Kingdom)", "en-GB"));
                _myLocals.Add(new TTS("French (France)", "fr-FR"));
                _myLocals.Add(new TTS("German (Germany)", "de-DE"));
                //_myLocals.Add(new TTS("Italian (Italy)", "it-IT"));
                _myLocals.Add(new TTS("Japanese (Japan)", "ja-JP"));
                //_myLocals.Add(new TTS("Polish (Poland)", "pl-PL"));
                //_myLocals.Add(new TTS("Portuguese (Brazil)", "pt-BR"));
                //_myLocals.Add(new TTS("Russian (Russia)", "ru-RU"));
                //_myLocals.Add(new TTS("Spanish (Spain)", "es-ES"));
            }
      
            public IEnumerable<TTS> Items()
            {
                return (IEnumerable<TTS>)_myLocals;
            }
        }
    }


    界面需要Textbolck几个,Combobox一个,按钮一个

    MainPage.xaml

    <phone:PhoneApplicationPage
        x:Class="Voice_C2.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
        xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        FontFamily="{StaticResource PhoneFontFamilyNormal}"
        FontSize="{StaticResource PhoneFontSizeNormal}"
        Foreground="{StaticResource PhoneForegroundBrush}"
        SupportedOrientations="Portrait" Orientation="Portrait"
        shell:SystemTray.IsVisible="True">
    
        <!--LayoutRoot 是包含所有页面内容的根网格-->
        <Grid x:Name="LayoutRoot" Background="WhiteSmoke">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
    
    
            <!--TitlePanel 包含应用程序的名称和页标题-->
            <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
                <TextBlock x:Name="Ti1" Text="语音测试" Style="{StaticResource PhoneTextNormalStyle}" 
    
    Margin="12,0" Foreground="#FF0081FF"/>
                <TextBlock x:Name="Ti2"  Text="听读" Margin="9,-7,0,0" Style="{StaticResource 
    
    PhoneTextTitle1Style}" Foreground="#FF3C3C3C"/>
            </StackPanel>
    
            <!--ContentPanel - 在此处放置其他内容-->
            <Grid x:Name="ContentPanel" Background="White" Margin="12,138,12,0" Grid.RowSpan="2">
                <TextBlock x:Name="displayText" HorizontalAlignment="Left" Margin="22,10,0,0" 
    
    TextWrapping="Wrap" Text="TextBlock" VerticalAlignment="Top" Width="424" FontSize="30" 
    
    Foreground="#FF3C3C3C"/>
                <!--<ScrollViewer Margin="0,250,0,105" VerticalScrollBarVisibility="Disabled">-->
                <ComboBox HorizontalAlignment="Left" Width="276" Name="voicesComboBox" 
    
    DisplayMemberPath="Name" Foreground="#FF3C3C3C" Margin="22,0,0,165" VerticalAlignment="Bottom" 
    
    Height="175" RenderTransformOrigin="0.5,0.5" >
                    <ComboBox.RenderTransform>
                        <CompositeTransform ScaleY="1"/>
                    </ComboBox.RenderTransform>
                </ComboBox>
                <!--</ScrollViewer>-->
                <Button Content="Speak to me" HorizontalAlignment="Left" Width="456" Height="80" 
    
    Margin="0,457,0,110" Visibility="Collapsed"/>
                <Button x:Name="Reco1" Content="点击开始聆听" HorizontalAlignment="Left" Height="80" 
    
    Margin="0,530,0,0" Grid.Row="1" VerticalAlignment="Bottom" Click="Reco1_Click" Width="456" 
    
    Background="#FF3C3C3C"/>
                <TextBlock x:Name="ansText" HorizontalAlignment="Left" Margin="19,55,0,0" TextWrapping="Wrap" 
    
    VerticalAlignment="Top" Width="424" FontSize="30" Height="142" Foreground="#FF3C3C3C"/>
                <RadioButton Content="Male" IsChecked="true" x:Name="MaleRadioButton" Margin="10,462,328,93" 
    
    Foreground="#FF3C3C3C" Background="#FF0081FF" VerticalAlignment="Bottom"/>
                <RadioButton Content="Female" Margin="133,462,183,93" Foreground="#FF3C3C3C" 
    
    Background="#FF0081FF" VerticalAlignment="Bottom"/>
            </Grid>
    
            <!--<Image Source="/Assets/AlignmentGrid.png" VerticalAlignment="Top" Height="800" Width="480" 
    
    Margin="0,-32,0,0" Grid.Row="0" Grid.RowSpan="2" IsHitTestVisible="False" />-->
        </Grid>
    
    </phone:PhoneApplicationPage>

    楼下一说提醒了我,的确是需要注册这两个:

    <Capability Name="ID_CAP_SPEECH_RECOGNITION" />

    <Capability Name="ID_CAP_MICROPHONE" />


    • 已编辑 Ruoshui Li 2015年4月3日 12:38 根据回复完善细节
    • 已建议为答案 Ruoshui Li 2015年12月13日 3:11
    2015年3月25日 15:05
  • Hello:

    我把你提供的代码放在一个demo中运行会错误,错误原因是在项目WMAppManifest文件中没有注册

     <Capability Name="ID_CAP_SPEECH_RECOGNITION" />

     <Capability Name="ID_CAP_MICROPHONE" />

    这两个capability,注册之后可以正常输入语音,会在InstalledVoices.All下面断掉,因为myLocal.Lcid为空。

    2015年3月25日 15:44
  • Hello:

    我把你提供的代码放在一个demo中运行会错误,错误原因是在项目WMAppManifest文件中没有注册

     <Capability Name="ID_CAP_SPEECH_RECOGNITION" />

     <Capability Name="ID_CAP_MICROPHONE" />

    这两个capability,注册之后可以正常输入语音,会在InstalledVoices.All下面断掉,因为myLocal.Lcid为空。

    第二段的TTS那个需要单独作为“TTS.cs”文件,是否正确写好了?
    2015年4月3日 12:34