iOS開発文字音声iOS 10

2547 ワード

フレームのインポート:

  import 

作成

   @interface ViewController ()
  {
      AVSpeechSynthesizer * av;
  }

ボタンの追加

/ 
UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];

button.frame=CGRectMake(100,100,100,50);

[button setTitle:@" "forState:UIControlStateNormal];

[button setTitle:@" "forState:UIControlStateSelected];

[button setTitleColor:[UIColor blueColor]forState:UIControlStateNormal];

button.backgroundColor=[UIColor grayColor];

button.showsTouchWhenHighlighted=YES;

[button addTarget:self action:@selector(start:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];

エージェントの設定と実装

  -(void)start:(UIButton*)sender{
if(sender.selected==NO) {
    if([av isPaused]) {
        // , 
        [av continueSpeaking];
        sender.selected=!sender.selected;
    }else{
        // 
        av= [[AVSpeechSynthesizer alloc]init];
        av.delegate=self;// 
        AVSpeechUtterance*utterance = [[AVSpeechUtterance alloc]initWithString:@"  How To Cover Your Tracks On The Internet2008-06-25  :Security Website Security - Creating a Bulletproof Site in 5 Easy Steps 2008-06"];// 
        
        utterance.rate=0.5;//  , 0-1, 0 ,1 ;
        AVSpeechSynthesisVoice*voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"zh-CN"];// , 
        utterance.voice= voice;
        [av speakUtterance:utterance];// 
        sender.selected=!sender.selected;
                }
}else{
    [av pauseSpeakingAtBoundary:AVSpeechBoundaryWord];// 
    sender.selected=!sender.selected;
}
  }
  - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didStartSpeechUtterance:(AVSpeechUtterance*)utterance{
      NSLog(@"--- ");
  }
  - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance{
      NSLog(@"--- ");
  }
  - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didPauseSpeechUtterance:(AVSpeechUtterance*)utterance{
      NSLog(@"--- ");
  }
  - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didContinueSpeechUtterance:(AVSpeechUtterance*)utterance{
      NSLog(@"--- ");
  }
    - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer     didCancelSpeechUtterance:(AVSpeechUtterance*)utterance{
      NSLog(@"--- ");
  }