
[英]How to stop voice Windows.Media.SpeechSynthesis when item deselected
[英]Creating a UWP DLL using Windows::Media::SpeechSynthesis
我目前正在尝试使用命名空间Windows :: Media :: SpeechSynthesis开发语音合成UWP DLL。 我阅读了本文档和专用于命名空间的Microsoft 页面 。 我试图在代码中实现命名空间。
头文件
#pragma once
#include <stdio.h>
#include <string>
#include <iostream>
#include <ppltasks.h>
using namespace Windows::Media::SpeechSynthesis;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::Media::Playback;
namespace SDKTemplate
{
class TextToSpeechDll
{
public:
__declspec( dllexport ) void ttsInitialize();
private:
MediaElement ^media;
};
}
Cpp文件
#include "stdafx.h"
#include "Dll2.h"
using namespace SDKTemplate;
using namespace Platform;
using namespace Concurrency;
void TextToSpeechDll::ttsInitialize()
{
SpeechSynthesizer ^synth = ref new SpeechSynthesizer();
// The object for controlling the speech synthesis engine (voice).
synth = ref new SpeechSynthesizer();
// The string to speak.
String^ text = "Hello World";
// Generate the audio stream from plain text.
task<SpeechSynthesisStream ^> speakTask = create_task( synth->SynthesizeTextToStreamAsync( text ) );
speakTask.then( [this, text]( task<SpeechSynthesisStream^> synthesisStreamTask )
{
SpeechSynthesisStream ^speechStream = synthesisStreamTask.get();
// Send the stream to the media object.
// media === MediaElement XAML object.
media->AutoPlay = true;
media->SetSource( speechStream, speechStream->ContentType );
media->Play();
} );
}
我可以加载DLL文件和我导出的函数。 但是,当我尝试调用该函数时,出现以下错误
我在微软页面上尝试了这个例子,但它有些不起作用,我无法弄清楚原因。 我还测试了Github上提供的Windows Universal Samples,这是一个重新组合文本到语音和语音识别的UWP应用程序。
有人经历过类似的问题吗? 当我没有接口时,我不应该使用XAML元素吗?
编辑1
我根据@Peter Torr-MSFT的建议修改了关于函数导出的头文件
#pragma once
#include <stdio.h>
#include <string>
#include <iostream>
#include <ppltasks.h>
using namespace Windows::Media::SpeechSynthesis;
using namespace Windows::UI::Xaml::Controls;
using namespace Windows::UI::Xaml::Media;
using namespace Windows::Media::Playback;
namespace SDKTemplate
{
public ref class TextToSpeechDll sealed
{
public:
void ttsInitialize();
private:
MediaElement ^media = ref new MediaElement();
};
}
但是,当我编译时,我在这一行上遇到了新的错误
speakTask.then( [this]( task<SpeechSynthesisStream^> synthesisStreamTask )
我研究了这个错误,如果我理解正确,它来自DLL函数的输入。
另外,我这样称呼函数
_ttsUwpDll->ttsInitialize();
这让我们来到这里
void NxWindowsTtsUwpDll::ttsInitialize()
{
int retVal = 0;
try
{
retVal = _ttsInitialize();
}
catch( ... )
{
printf( "Exception in ttsInitialize\n" );
}
//return retVal;
}
在MainPage中,我初始化dll文件并调用“ttsInitialize”函数,如下面的代码。
MainPage::MainPage()
{
InitializeComponent();
TextToSpeechDll* gf = new TextToSpeechDll();
gf->ttsInitialize();
}
在Dll.h文件中,我像下面的代码一样初始化MediaElement,其他代码和你一样。
MediaElement^ media = ref new MediaElement();
当我运行项目时,它的工作原理。 您可以尝试一下,如果仍有问题,请显示初始化dll文件的详细信息。
我找到了一个问题的答案。 我没有使用MediaElement
,而是使用了MediaPlayer
。 现在它可以工作了,但是我仍然需要弄清楚如何在不限制它的情况下使发动机说话。 Sleep( 3000 )
表示语音将说话3秒钟。 但是,如果句子超过3秒,它将被删除。 这是程序的代码。
int TextToSpeechUwpDll::ttsSpeak( const char* text )
{
SpeechSynthesizer ^speak = ref new SpeechSynthesizer();
MediaPlayer ^player = ref new MediaPlayer;
int wchars_num = MultiByteToWideChar( CP_ACP, 0, text, -1, NULL, 0 );
wchar_t* texts = new wchar_t[wchars_num];
MultiByteToWideChar( CP_ACP, 0, text, -1, texts, wchars_num );
String ^sentence = ref new String( texts );
task<SpeechSynthesisStream ^> speakTask = create_task( speak->SynthesizeTextToStreamAsync( sentence ) );
speakTask.then( [player, sentence]( SpeechSynthesisStream ^speechStream )
{
player->Source = MediaSource::CreateFromStream( speechStream, speechStream->ContentType );
player->AutoPlay = false;
player->Play();
Sleep( 3000 );
} );
return true;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.