如何使用 iOS 7 的 AVSpeechSynthesizer 国家有声读物(4)

控制:我们一定要学会控制

尤达大师(电影《星球大战》)有话:的关键在于控制。这本故事书是一个字一个字读出来,我愿意为它添加两个button,音调和语速,以便我们能够调整语音合成实时的时候。

还是 RWTPageViewController.m,在nextSpeechIndex 属性后声明下列属性:

@property (nonatomic, assign) float currentPitchMultiplier;

@property (nonatomic, assign) float currentRate;

在 gotoPreviousPage:方法后添加例如以下方法:

- (void)lowerPitch {

   if (self.currentPitchMultiplier > 0.5f) {

     self.currentPitchMultiplier = MAX(self.currentPitchMultiplier * 0.8f, 0.5f);

   }

}

- (void)raisePitch {

   if (self.currentPitchMultiplier < 2.0f) {

     self.currentPitchMultiplier = MIN(self.currentPitchMultiplier * 1.2f, 2.0f);

   }

}  

- (void)lowerRate {

   if (self.currentRate > AVSpeechUtteranceMinimumSpeechRate) {

     self.currentRate = MAX(self.currentRate * 0.8f, AVSpeechUtteranceMinimumSpeechRate);

   }

}  

- (void)raiseRate {

   if (self.currentRate < AVSpeechUtteranceMaximumSpeechRate) {

     self.currentRate = MIN(self.currentRate * 1.2f, AVSpeechUtteranceMaximumSpeechRate);

   }

}  

-(void) speakAgain {

     if (self.nextSpeechIndex == [[self currentPage].utterances count]) {

       self.nextSpeechIndex = 0;

       [self speakNextUtterance];

     }

}

这些方法将和语音控制面板上的button相连接。

  • lowerPitch: 和 raisePitch: 方法用于减少和提高音高。每次都会在原有基础上减低或添加20%。但该值会限定在(0.5-2.0)之间。
  • lowerRate: 和 raiseRate 方法用于减少和提高朗读的语速,每次会在原有基础上+/- 20%,整体范围限定在 AVSpeechUtteranceMinimumSpeechRate 和 AVSpeechUtteranceMaximumSpeechRate 之间。

  • speakAgain: 重置当前语句的索引值,并再次显示对应的文字内容。

创建button,然后在 raiseRate 方法后加入下列方法:

-(void) addSpeechControlWithFrame: (CGRect) frame title:(NSString *) title action:(SEL) selector {

   UIButton *controlButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

   controlButton.frame = frame;

   controlButton.backgroundColor = [UIColor colorWithWhite:0.9f alpha:1.0f];

   [controlButton setTitle:title forState:UIControlStateNormal];

   [controlButton addTarget:self

                  action:selector        forControlEvents:UIControlEventTouchUpInside];

   [self.view addSubview:controlButton];

}  

- (void)addSpeechControls {

   [self addSpeechControlWithFrame:CGRectMake(52, 485, 150, 50)

                              title:@"Lower Pitch"

                             action:@selector(lowerPitch)];

   [self addSpeechControlWithFrame:CGRectMake(222, 485, 150, 50)

                              title:@"Raise Pitch"

                             action:@selector(raisePitch)];

   [self addSpeechControlWithFrame:CGRectMake(422, 485, 150, 50)

                              title:@"Lower Rate"

                             action:@selector(lowerRate)];

   [self addSpeechControlWithFrame:CGRectMake(592, 485, 150, 50)

                              title:@"Raise Rate"

                             action:@selector(raiseRate)];

   [self addSpeechControlWithFrame:CGRectMake(506, 555, 150, 50)

                              title:@"Speak Again"

                             action:@selector(speakAgain)];  

}

addSpeechControlWithFrame:方法是一个便利方法。用于在视图中加入一个button并将其与对应的方法连接起来,这样就能够通过button来调整语音。

注意:也能够在 Main.storyboard 中创建button并绑定他们的action 到 RWTPageViewController。这样更easy,效率也更高。

在 viewDidLoad 的 [self startSpeaking] 方法之前加入:

    // 1

   self.currentPitchMultiplier = 1.0f;

   self.currentRate = AVSpeechUtteranceDefaultSpeechRate;

     // 2

   [self addSpeechControls];

凝视“1”处设置默认的语音属性。凝视“2”处则加入语音控制button。

 最后,改动  speakNextUtterance    方法:

- (void)speakNextUtterance {

   if (self.nextSpeechIndex < [[self currentPage].utterances count]) {

     AVSpeechUtterance *utterance = [[self currentPage].utterances objectAtIndex:self.nextSpeechIndex];

     self.nextSpeechIndex    += 1;

       // 1

     utterance.pitchMultiplier = self.currentPitchMultiplier;

     // 2

     utterance.rate = self.currentRate;

       [self.synthesizer speakUtterance:utterance];

   }

}

假设你点击了 lower/raise button。则新设置的值将在下一句朗读中得到应用。

编译执行。例如以下图所看到的:

触摸或点击这些button,然后注意发音的变化。

尤达确实非常厉害,哪怕你不是杰迪也能够成为大师(AVSpeechSynthesizer 方面的)。

结尾

这里能够下载 完整的项目代码

希望本文能成为激发你开发自己的有声书的动力。假设你想知道很多其它的关于怎样对合成语音进行调优的技巧,请看以下:

最佳有声书WhirlySquirrelly.plist竞赛

说明:请尝试进一步调优 WhirlySquirrelly.plist,并上传至论坛或者本文留言里。

我们会评出当中的优胜者,并在评论中加以褒扬。

同意用户选择图书

说明: 加入一个“Choose Book” button。并在 UIPopoverController中显示一个可选的图书列表。当用户选择某本图书,在 RWTPageViewController 中重置 book 对象并显示新书。

从 Web 上下载图书

说明:将书籍以 plist 格式存储在 webserver或者提供相似 AWS S3 或者 Heroku 的服务。server先要提供一个 url列表。列出全部图书,然后再提供一个能够下载某本书的服务。

在前一个功能中,将图书的链接加入进去。

念到词高亮显示

提示: 使用AVSpeechSynthesizerDelegate 托付中的方法

在 speechSynthesizer:didStartSpeechUtterance:方法中。高亮指定的 utterance。

在 speechSynthesizer:didFinishSpeechUtterance:方法中。将高亮的 utterance 去高亮。你能够用 pageTextLable 的 attributedText 属性,通过NSAttributedString 设置不同的背景色和字体属性以实现加亮效果。

在第一页显示书名

Addthe Ability to Display a Title Page Before All Other Pages

说明:在 RWTPageViewController 之前加入额外的 viewController,并设置 Main.storyboard 新的属性已启用viewcontroller。他们俩 ViewController 随着 UINavigationController 经营。 为此,您可以更改 Page 类的设计,并分成声 Page 和沉默 Page,然后换 RWTPageViewController 我们可以用这两种不同的处理Page。


原文地址:https://www.cnblogs.com/lcchuguo/p/4801556.html