[英]Open ears text to speech (voice) not working when getting string from another class/controller (IOS, Objective c)
I am very new to objective c and OpenEars so please forgive me if I have some messy code and if I am lost in very simple problem.我对objective c和OpenEars很陌生,所以如果我有一些混乱的代码并且我迷失在非常简单的问题中,请原谅我。
Anyhow, I have two controllers in this application.无论如何,我在这个应用程序中有两个控制器。 The first being the default ViewController and the second one being a new one that I made called ReplyManagerController.
第一个是默认的 ViewController,第二个是我创建的名为 ReplyManagerController 的新控制器。
The code in the ViewController basically uses the one in the tutorial with some (maybe more some) changes. ViewController 中的代码基本上使用教程中的代码,并进行了一些(可能更多一些)更改。
EDIT:编辑:
The app is supposed to be a basic app where a user says something and the app replies.该应用程序应该是一个基本的应用程序,用户说一些东西,应用程序回复。
But the original problem was that I could not get the string to display or TTS to work when my ViewController got it's string from another class/controller.但最初的问题是,当我的 ViewController 从另一个类/控制器获取字符串时,我无法显示字符串或 TTS 工作。
The answer in my below mentions that it was probably because my other class was calling my ViewController without the self.fliteController initialized.我下面的答案提到这可能是因为我的另一个类在没有初始化 self.fliteController 的情况下调用了我的 ViewController。
How would I initialize the ViewController with self.fliteController initialized?我将如何使用初始化的 self.fliteController 来初始化 ViewController?
ViewController.m视图控制器.m
- (void) pocketsphinxDidReceiveHypothesis:(NSString *)hypothesis recognitionScore:(NSString *)recognitionScore utteranceID:(NSString *)utteranceID {
NSString *strResult = hypothesis; //speech to text to string
ReplyManager* replyObject = [[ReplyManager alloc] init];
[replyObject speechResult:(NSString*)strResult viewController:self];
}
- (void) getReply:(NSString*)replyStr{
[self.fliteController say:replyStr withVoice:self.slt];
[self updateText:replyStr];
}
- (IBAction)updateText:(NSString*)replyStr{
labelOne.text = replyStr;
labelOne.adjustsFontSizeToFitWidth = YES;
labelOne.minimumFontSize = 0;
}
Any help will be great!任何帮助都会很棒! Thanks!
谢谢!
ReplyManager.m回复经理.m
- (void) speechResult:(NSString*)strResult {
NSString *replystr;
NSString *greetings = @"Hi!";
NSString *sorry = @"Sorry I didn't catch that?";
ViewController* getReply = [[ViewController alloc] init];
if ([strResult isEqualToString:@"HELLO"])
{
replystr = greetings;
[getReply getReply:(NSString*)replystr];
}
else
{
replystr = sorry;
[getReply getReply:(NSString*)replystr];
}
}
EDIT 2:编辑2:
viewDidLoad Method viewDidLoad 方法
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.fliteController = [[OEFliteController alloc] init];
self.slt = [[Slt alloc] init];
self.openEarsEventsObserver = [[OEEventsObserver alloc] init];
[self.openEarsEventsObserver setDelegate:self];
}
ViewController* getReply = [[ViewController alloc] init];
Here you init a new ViewController
which does not have self.fliteController
defined most likely.在这里,您初始化一个新的
ViewController
,它最有可能没有定义self.fliteController
。 You need to reuse previos controller, for example like this:您需要重用 previos 控制器,例如像这样:
[replyObject speechResult:(NSString*)strResult viewController:self];
So you can use already initialized viewController
later.所以你可以稍后使用已经初始化的
viewController
。 Overall it is better to initialize objects like viewController
or replyController
beforehand, not inside callback methods.总的来说,最好事先初始化像
viewController
或replyController
这样的对象,而不是在回调方法中。
It sounds like a timing issue where you're trying to use fliteController
before it's been initialized.听起来像是在初始化之前尝试使用
fliteController
的计时问题。
In your ViewController
class, where do you assign a value to the fliteController
property?在您的
ViewController
类中,您在哪里为fliteController
属性赋值? In an initializer?在初始化程序中?
-(void)viewDidLoad
? -(void)viewDidLoad
?
In ReplyManagerController
add:在
ReplyManagerController
添加:
ViewController* getReply = [[ViewController alloc] init];
// Add these lines
NSLog(getReply.fliteController); // Is this nil?
[getReply.view layoutIfNeeded];
NSLog(getReply.fliteController); // Is it still nil?
Does the above fix the problem?以上是否解决了问题? If so, you're probably initializing
fliteController
in -(void)viewDidLoad
.如果是这样,您可能正在
-(void)viewDidLoad
初始化fliteController
。 What's the result of the two NSLog
statements?两个
NSLog
语句的结果是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.