2017-09-27 1 views
0

私はWindows.Media.SpeechSynthesisクラスでアプリケーションが最初の文字列を合成するという、この単純なコードをC#UWPに入れています。最初の合成が終了した後の文字列。私は、文字列1 + str2のを含めるだけの文字列を作ることでそれが可能だ知っているが、このコードを実行するシナリオはより複雑であり、これは、それは不可能です。いつものように(英語の私の低レベルのために申し訳ありません)どのように私は非同期関数の終わりに関数を開始するのですか?C#UWP

public MainPage() 
    { 
     this.InitializeComponent(); 
     string str1 = "weather data"; 
     talk(Textmeteo); 
     string str2 = "hello world"; 
     talk(str2); 
    } 

    public async void talk(string text) 
    { 
     // The media object for controlling and playing audio. 
     MediaElement mediaElement = new MediaElement(); 

     // The object for controlling the speech synthesis engine (voice). 
     SpeechSynthesizer synth = new SpeechSynthesizer(); 

     // Generate the audio stream from plain text. 


     SpeechSynthesisStream stream = await synth.SynthesizeTextToStreamAsync(text); 

     // Send the stream to the media object. 
     mediaElement.SetSource(stream, stream.ContentType); 
     mediaElement.Play(); 

    } 
+0

なぜあなたのシナリオでは 'str1 + str2'はできませんか? – AVK

+0

@AVK str1が合成されている最初のシナリオでは、str2を構成するデータをオンラインで読むためにhttp要求が実行されるためです。 2番目のシナリオでは、私は音声認識を開始するために、最初にsynthetizeの最後を指すデータが必要です。 –

答えて

2

asyncメソッドをTaskまたはTask<T>の代わりにvoidの代わりに返すことをお勧めします。
あなたがTaskを返却するとき、あなたは、単にContinueWithと継続を追加することができます。

public MainPage() 
{ 
    this.InitializeComponent(); 
    string str1 = "weather data"; 
    Task talkingTask = talk(Textmeteo); 
    string str2 = "hello world"; 
    talkingTask = talkingTask.ContinueWith(completedTask => talk(str2)); 
} 

public async Task talk(string text) 
{ 
    // await... 
} 
+0

私は使用法を教えてください:talkingTask = talkingTask.ContinueWith(completedTask => talk(str2)); –

+0

@MilleHobbyそれだけのこと。 'async talk'メソッドは' Task'(これはコンパイラによって行われます)を返し、 'ContinueWith'は前のアクションが完了したときに次の' Action'(パラメータ)を開始するように指示します。 –

+0

もし10個の文字列があれば、 'ContinueWith'を10回繰り返す必要がありますか? – AVK

2

は、メソッドがTaskなくvoidを返しtalkください。

public MainPage() 
{ 
    this.InitializeComponent(); 
    MakeTalk(); 
} 

private async void MakeTalk() 
{ 
    // Surround by a try catch as we have async void. 
    string str1 = "weather data"; 
    await talk(Textmeteo); 
    string str2 = "hello world"; 
    await talk(str2); 
} 

public async Task talk(string text) 
{ 
    // [...] 
} 
関連する問題