2013年10月17日
川俣晶の縁側ソフトウェア技術雑記 total 6445 count

Windows Store Appで喋らせる方法

Written By: 川俣 晶連絡先

 とりあえず、ワンフレーズ喋るだけ。

 とっても簡単。

C#のコード §

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using System.Runtime.InteropServices.WindowsRuntime;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

using Windows.Media.SpeechSynthesis;

namespace Speech

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        private async void PushMe_Click(object sender, RoutedEventArgs e)

        {

            MediaElement mediaElement = this.media;

            var synth = new Windows.Media.SpeechSynthesis.SpeechSynthesizer();

            SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync("そのコマンドは入力されていません。");

            mediaElement.SetSource(stream, stream.ContentType);

            mediaElement.Play();

        }

    }

}

XAMLのコード §

<Page

    x:Class="Speech.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:Speech"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <Button x:Name="PushMe" FontSize="40" Click="PushMe_Click">Push Me</Button>

        <MediaElement HorizontalAlignment="Left" Height="100" VerticalAlignment="Top" Width="100" x:Name="media"/>

    </Grid>

</Page>