SpeakProgressEventArgs.CharacterPosition 属性
定义
获取从提示开始到刚刚说出的单词的第一字母之前的位置的字符与空格数。Gets the number of characters and spaces from the beginning of the prompt to the position before the first letter of the word that was just spoken.
public:
property int CharacterPosition { int get(); };
public int CharacterPosition { get; }
member this.CharacterPosition : int
Public ReadOnly Property CharacterPosition As Integer
属性值
返回从提示符开始到刚刚讲出的单词的第一个字母前的字符和空格数。Returns the number of characters and spaces from the beginning of the prompt to the position before the first letter of the word that was just spoken.
示例
下面的示例创建一个 PromptBuilder ,并使用追加 XML 文件的 SSML 内容 XmlReader 。The following example creates a PromptBuilder and appends the SSML contents of an XML file using XmlReader. 该示例将语音输出到 WAV 文件以进行播放。The example outputs speech to a WAV file for playback. 包含 SSML 的 XML 文件的内容将显示在代码示例下面。The contents of the XML file containing the SSML appear below the code example.
using System;
using System.Xml;
using System.IO;
using System.Speech.Synthesis;
namespace SampleSynthesis
{
class Program
{
static void Main(string[] args)
{
// Initialize a new instance of the SpeechSynthesizer.
using (SpeechSynthesizer synth = new SpeechSynthesizer())
{
// Configure the audio output.
synth.SetOutputToDefaultAudioDevice();
// Create a path to the file that contains SSML.
string weatherFile = Path.GetFullPath("c:\\test\\Weather.ssml");
// Create an XML Reader from the file, create a PromptBuilder and
// append the XmlReader.
PromptBuilder builder = new PromptBuilder();
if (File.Exists(weatherFile))
{
XmlReader reader = XmlReader.Create(weatherFile);
builder.AppendSsml(reader);
reader.Close();
}
// Add a handler for the SpeakProgress event.
synth.SpeakProgress +=
new EventHandler<SpeakProgressEventArgs>(synth_SpeakProgress);
// Speak the prompt and play back the output file.
synth.Speak(builder);
}
Console.WriteLine();
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
}
// Write each word and its character position to the console.
static void synth_SpeakProgress(object sender, SpeakProgressEventArgs e)
{
Console.WriteLine("Speak progress: {0} {1}",
e.CharacterPosition, e.Text);
}
}
}
<!-- The following are the contents of the file Weather.ssml.
Note that because of the <p> tag and the space that follows it,
that the character position of the first word "The" will be 86. -->
<?xml version="1.0" encoding="ISO-8859-1"?>
<speak version="1.0"
xmlns="http://www.w3.org/2001/10/synthesis"
xml:lang="en-US">
<p> The weather forecast for today is partly cloudy with
some sun breaks. </p>
<break strength="medium" />
<p> Tonight's weather will be cloudy with a 30% chance of
showers. </p>
</speak>
注解
CharacterPosition包括 XML 标记中字符的计数,包括它们的括在括号中。The CharacterPosition includes the count for characters in XML tags, including their enclosing brackets. 使用 AppendText 、 AppendTextWithAlias 、 AppendTextWithHint 、 AppendSsmlMarkup 或 AppendTextWithPronunciation 方法中的任何一个时,会将内容添加到包含开始和结束元素的 SSML 提示符下 speak 。When using any of the AppendText, AppendTextWithAlias, AppendTextWithHint, AppendSsmlMarkup, or AppendTextWithPronunciation methods, the contents are added to an SSML prompt that includes the opening and closing speak elements. 开始 speak 元素向 CharacterPosition 提示符中所有单词和字母的中添加了82个字符和空格的偏移量。The opening speak element adds an offset of 82 characters and spaces to the CharacterPosition of the all the words and letters in the prompt. 例如,在以下代码片段中, CharacterPosition 第一个单词 "this" 的是82。For example, in the following snippet, the CharacterPosition of the first word, "this", is 82.
builder.AppendText("This is a test");
Synthesizer.Speak(builder);
在上面的示例中, CharacterPosition "test" 一词为92。In the above example the CharacterPosition of the word "test" is 92. 在下面的代码片段中," CharacterPosition test" 一词的 "test" 为23个字符 (115) ,因为 <prosody pitch="high"> 它前面的开始标记包含23个字符和空格 (这两个转义符 " \ " 不计入) 。In the following snippet the CharacterPosition of the word "test" is 23 characters higher (115) because the opening <prosody pitch="high"> tag that precedes it contains 23 characters and spaces (the two escape characters "\" are not counted).
builder.AppendSsmlMarkup("This is a <prosody pitch=\"high\"> test </prosody>.");
Synthesizer.Speak(builder);
如果 AppendSsml 通过指定文件来使用向提示添加内容的方法,则 xml speak 不会使用文件中的开始声明和元素,也不会对其进行计数。If you use the AppendSsml methods to add content to a prompt by specifying a file, the opening xml declaration and speak elements in the file are not used or counted. 开始标记后,文件中的第一个字符 speak 位于位置82(如果它是提示中的第一个内容)。The first character in the file after the opening speak tag will be at position 82 if it is the first content in the prompt.
与此相反,在 Speak 朗读之前,不会将方法的字符串参数添加到 SSML 提示符下。By contrast, the string parameter of a Speak method does not get added to an SSML prompt before being spoken. 因此, CharacterPosition 以下代码片段中第一个单词 "this" 的是零。Therefore, the CharacterPosition of the first word, "this", in the following snippet is zero.
Synthesizer.Speak("This is a test.");
将 SpeechSynthesizer 数字规范化为与数字的讲述方式相对应的单词。The SpeechSynthesizer normalizes numbers to the words that correspond to how the number will be spoken. 例如,合成器将数字 "4003" 说为 "4003"。For example, the synthesizer speaks the number "4003" as "four thousand three". 它 SpeakProgress 为三个口述单词中的每一个都引发一个事件。It raises a SpeakProgress event for each of the three spoken words. 但是, CharacterPosition 这三个单词中的每一个都是相同的。However, the CharacterPosition property for each of the three words is the same. 它是提示文本中第一个字符 "4003" 之前的位置。It is the position before the first character of the number "4003" in the text of the prompt.