使用 Windows::Media::SpeechSynthesis 创建 UWP DLL [英] Creating a UWP DLL using Windows::Media::SpeechSynthesis
问题描述
我目前正在尝试使用命名空间 Windows::Media::SpeechSynthesis 开发语音合成 UWP DLL.我读了这个
我在 Microsoft
我研究了这个错误,如果我理解正确的话,它来自于 DLL 函数的导入.
另外,我这样调用函数
_ttsUwpDll->ttsInitialize();
是什么把我们带到了这里
void NxWindowsTtsUwpDll::ttsInitialize(){int retVal = 0;尝试{retVal = _ttsInitialize();}抓住( ... ){printf("ttsInitialize 中的异常\n");}//返回retVal;}
我找到了问题的答案.我没有使用 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 );任务speakTask = create_task( speak->SynthesizeTextToStreamAsync( sentence ) );speakTask.then( [玩家,句子]( SpeechSynthesisStream ^speechStream ){player->Source = MediaSource::CreateFromStream(speechStream,speechStream->ContentType);播放器->自动播放=假;播放器->播放();睡眠(3000);});返回真;}
I am currently trying to develop a speech synthesis UWP DLL using the namespace Windows::Media::SpeechSynthesis. I read this documentation and the Microsoft page dedicated to the namespace. I tried to implement the namespace in code.
Header file
#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 file
#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();
} );
}
I can load the DLL file and the function I exported. However, when I try to call the function, I get following error
I tried the example on the Microsoft page but it some how doesn't work and I can't figure out why. I also tested the Windows Universal Samples available on Github which is an UWP app regrouping Text-To-Speech and Speech Recognition.
Has someone experienced a similar issue? Shouldn't I use an XAML element when I don't have an interface?
Edit 1
I modified the header file regarding the exportation of the function as suggested by @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();
};
}
However, when I compile, I'm getting a new error on this line
speakTask.then( [this]( task<SpeechSynthesisStream^> synthesisStreamTask )
I researched this error and if I understood it correctly it comes from the importation of the DLL function.
In addition, I call the function like this
_ttsUwpDll->ttsInitialize();
Which brings us here
void NxWindowsTtsUwpDll::ttsInitialize()
{
int retVal = 0;
try
{
retVal = _ttsInitialize();
}
catch( ... )
{
printf( "Exception in ttsInitialize\n" );
}
//return retVal;
}
I found an answer to my question. Instead of using MediaElement
, I used MediaPlayer
. Now it works, but I still need to figure out how to make the engine speak without limiting it in time. The Sleep( 3000 )
means that the voice will speak for 3 seconds. However, if the sentence is longer than 3 seconds, it will be cut.
Here is the code of the program.
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;
}
这篇关于使用 Windows::Media::SpeechSynthesis 创建 UWP DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!