惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
G
GRAHAM CLULEY
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V2EX - 技术
V2EX - 技术
The Last Watchdog
The Last Watchdog
S
Secure Thoughts
Webroot Blog
Webroot Blog
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
Hacker News: Ask HN
Hacker News: Ask HN
N
News and Events Feed by Topic
H
Heimdal Security Blog
H
Help Net Security
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
The GitHub Blog
The GitHub Blog
Jina AI
Jina AI
Recent Commits to openclaw:main
Recent Commits to openclaw:main
F
Full Disclosure
小众软件
小众软件
S
Securelist
罗磊的独立博客
NISL@THU
NISL@THU
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cisco Blogs
云风的 BLOG
云风的 BLOG
C
CERT Recently Published Vulnerability Notes
Cisco Talos Blog
Cisco Talos Blog
Know Your Adversary
Know Your Adversary
S
Schneier on Security
D
DataBreaches.Net
M
MIT News - Artificial intelligence
V
Vulnerabilities – Threatpost
N
News and Events Feed by Topic
有赞技术团队
有赞技术团队
F
Fortinet All Blogs
T
Tenable Blog
The Register - Security
The Register - Security
C
Check Point Blog
AWS News Blog
AWS News Blog
Cloudbric
Cloudbric
C
CXSECURITY Database RSS Feed - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
C
Cyber Attacks, Cyber Crime and Cyber Security
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google Online Security Blog
Google Online Security Blog
博客园 - 叶小钗
Hacker News - Newest:
Hacker News - Newest: "LLM"
博客园 - 司徒正美

博客园 - dalgleish

C# Avalonia 23- OpenGL- 例子通用类 C# Avalonia 22- SoundAndVideo- SpeechVoiceTest C# Avalonia 22- SoundAndVideo- SpeechSynthesis C# Avalonia 22- SoundAndVideo- CodePlayback C# Avalonia 22- SoundAndVideo- SoundPlayerTest C# Avalonia 21- MenusAndToolbars- RibbonTest C# Avalonia 21- MenusAndToolbars- ProportionalStatusBar C# Avalonia 21- MenusAndToolbars- BasicToolbar C# Avalonia 21- MenusAndToolbars- ToolBar/Ribbon开源项目介绍和修复 C# Avalonia 21- MenusAndToolbars- SidebarMenu C# Avalonia 21- MenusAndToolbars- MixedMenus C# Avalonia 20 - WindowsMenu- 魔改Hyperlink - 使用例子 C# Avalonia 20 - WindowsMenu- 魔改Hyperlink C# Avalonia 20 - WindowsMenu- WebViewTest C# Avalonia 20 - WindowsMenu- WebView C# Avalonia 20 - WindowsMenu- ModernWindowTest C# Avalonia 20 - WindowsMenu- ModernWindow C# Avalonia 20 - WindowsMenu- TransparentWithShapes C# Avalonia 20 - WindowsMenu- TransparentBackground C# Avalonia 20 - WindowsMenu- OpenFileTest C# Avalonia 20 - WindowsMenu- SavePostion C# Avalonia 20 - WindowsMenu- CenterScreen C# Avalonia 19- DataBinding- DataGridGrouping C# Avalonia 19- DataBinding- DirectoryTreeView C# Avalonia 19- DataBinding- BoundTreeView C# Avalonia 19- DataBinding- CustomListViewTest
C# Avalonia 22- SoundAndVideo- SpeechRecognition
dalgleish · 2026-07-17 · via 博客园 - dalgleish

SpeechRecognition.axaml代码

<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
        x:Class="AvaloniaUI.SpeechRecognition"
        Title="SpeechRecognition">
    <StackPanel Margin="10" Spacing="8">
        <Label x:Name="lbl" Content="空闲..." />
        <TextBlock Text="Try: '红色开', '蓝色关' ..." Opacity="0.7"/>
    </StackPanel>
</Window>

SpeechRecognition.axaml.cs代码

using Avalonia;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Avalonia.Threading;
using System;
using System.Globalization;
using System.Linq;
using System.Speech.Recognition;

namespace AvaloniaUI;

public partial class SpeechRecognition : Window
{
    private SpeechRecognitionEngine? recognizer;

    public SpeechRecognition()
    {
        InitializeComponent();
        try
        {
            var engine = CreateBestAvailableRecognizer();
            if (engine is null)
            {
                SetLabel("No System.Speech recognizer installed on this machine.");
                return;
            }

            recognizer = engine;

            var grammar = new GrammarBuilder();
            grammar.Append(new Choices("红色", "蓝色", "绿色", "黑色", "白色"));
            grammar.Append(new Choices("开", "关"));

            recognizer.LoadGrammar(new Grammar(grammar));

            recognizer.SpeechDetected += (_, _) => SetLabel("Speech detected.");
            recognizer.SpeechHypothesized += (_, _) => SetLabel("Speech uncertain.");
            recognizer.SpeechRecognitionRejected += (_, _) => SetLabel("Speech rejected.");
            recognizer.SpeechRecognized += (_, e) => SetLabel("You said: " + e.Result.Text);

            recognizer.SetInputToDefaultAudioDevice();
            recognizer.RecognizeAsync(RecognizeMode.Multiple);

            SetLabel($"Ready. Recognizer: {recognizer.RecognizerInfo.Culture.Name}");
        }
        catch (Exception ex)
        {
            SetLabel("Init failed: " + ex.Message);
        }
    }

    private SpeechRecognitionEngine? CreateBestAvailableRecognizer()
    {
        var infos = SpeechRecognitionEngine.InstalledRecognizers();
        if (infos is null || infos.Count == 0)
            return null;

        // 你可以按需调整优先级
        var preferred = new[] { "zh-CN", "en-US", "en-GB" };

        var best =
            infos.FirstOrDefault(x => preferred.Contains(x.Culture.Name, StringComparer.OrdinalIgnoreCase))
            ?? infos.First();

        return new SpeechRecognitionEngine(best);
    }

    private void SetLabel(string text) => lbl.Content = text;

    protected override void OnClosed(EventArgs e)
    {
        try
        {
            if (recognizer is not null)
            {
                recognizer.RecognizeAsyncCancel();
                recognizer.RecognizeAsyncStop();
                recognizer.Dispose();
            }
        }
        catch { }

        base.OnClosed(e);
    }
}

运行效果 - 语音识别

image