【发布时间】:2019-10-25 17:11:48
【问题描述】:
我目前正在尝试使用命名空间 Windows::Media::SpeechSynthesis 开发语音合成 UWP DLL。我读过这个documentation 和微软page 专用于命名空间。我试图在代码中实现命名空间。
头文件
#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 文件和我导出的函数。但是,当我尝试调用该函数时,出现以下错误
我在 Microsoft page 上尝试了该示例,但它有些不起作用,我不知道为什么。我还测试了Github 上提供的 Windows 通用示例,这是一个重新组合文本到语音和语音识别的 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;
}
【问题讨论】:
-
为什么您使用导出的 C++ 类而不是 WinRT 组件?你能展示你是如何初始化和使用这个类的吗?例如,在给出的代码中,
media从未分配给。 -
@PeterTorr-MSFT 我编辑了我的帖子(编辑 1)关于你问我的问题。你能解释一下我如何分配变量
media吗? -
我相信
MediaElement在使用前需要在 XAML 树中。您要实现的总体方案是什么? -
@PeterTorr-MSFT 我正在尝试制作一个 DLL 文件。你认为它可以与 MediaPlayer 元素一起使用吗?
-
“制作一个DLL”是如何,而不是what。为什么是 DLL,而不是 LIB 或 EXE?
MediaElement有什么用途?MediaPlayer是更好的选择吗?等
标签: uwp text-to-speech c++-cx speech-synthesis