//
//  TextToSpeechManager.m
//  HelloWorld
//
//  Created by JohnnyChung on 07/12/2016.
//  Copyright © 2016 Facebook. All rights reserved.
//

#import "TextToSpeechManager.h"
#import <AVFoundation/AVSpeechSynthesis.h>
#import <AVFoundation/AVAudioPlayer.h>


@implementation TextToSpeechManager


AVSpeechSynthesizer *synthesizer;

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(textToSpeechWithCallback:(NSString *)text callback:(nonnull RCTResponseSenderBlock)callback) {

  if(synthesizer == nil){
      synthesizer = [[AVSpeechSynthesizer alloc]init];
  }
  
  [synthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
  
  AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:text];
  AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];
  utterance.voice = voice;
  [synthesizer speakUtterance:utterance];
  callback(@[]);
}

@end
