如何使用 iOS 7 的 AVSpeechSynthesizer 製作有聲書(3)

來源:互聯網
上載者:User

標籤:style   http   color   使用   strong   檔案   

plist 中的每一頁 utteranceSting 我們都建立了一個RWTPage.displayText。因此,每頁的文本會一次性地顯示出來。

由於

You’ve constructedeach RWTPage.displayTextfrom the combined utteranceStringsfor the page in the plist. So, your page view displays the entire page’s text.
However, remember that RWTPageViewController.speakNextUtterancecreates a single AVSpeechUtterancefor the entire RWTPage.displayText.The result is that it overlooks your carefully parsed utterance properties.

In order to modifyhow each utterance is spoken, you need to synthesize each page’s text asindividual utterances. If only there were some way to observe and control howand when AVSpeechSynthesizerspeaks. Hmmm…

實現委託

AVSpeechSynthesizer 有一個委託AVSpeechSynthesizerDelegate ,在合成器生命週期中它負責通知各種重要的事件和動作。通過實現這些委託方法,我們可以讓聲音更自然。

開啟 RWTPage.h,加入下列代碼:

  @property (nonatomic, strong, readonly) NSArray *utterances;

開啟 RWTPage.m ,加入下列代碼:

  @property (nonatomic, strong, readwrite) NSArray *utterances;

注意這個技巧:將一個屬性在標頭檔中聲明為唯讀,而在實現檔案中聲明為可讀可寫。這相當於只有對象自己能夠寫這個屬性。

將 pageWithAttribute:方法修改為:

+ (instancetype)pageWithAttributes:(NSDictionary*)attributes {

   RWTPage *page = [[RWTPage alloc] init];

     if ([[attributes objectForKey:RWTPageAttributesKeyUtterances] isKindOfClass:[NSString class]]) {

     page.displayText = [attributes objectForKey:RWTPageAttributesKeyUtterances];

     page.backgroundImage = [attributes objectForKey:RWTPageAttributesKeyBackgroundImage];

     // 1

     page.utterances  = @[[[AVSpeechUtterance alloc] initWithString:page.displayText]];

   } else if ([[attributes objectForKey:RWTPageAttributesKeyUtterances] isKindOfClass:[NSArray class]]) {

     NSMutableArray *utterances = [NSMutableArray arrayWithCapacity:31];

     NSMutableString *displayText = [NSMutableString stringWithCapacity:101];

       for (NSDictionary *utteranceAttributes in [attributes objectForKey:RWTPageAttributesKeyUtterances]) {

       NSString *utteranceString =                  [utteranceAttributes objectForKey:RWTUtteranceAttributesKeyUtteranceString];

       NSDictionary *utteranceProperties =                      [utteranceAttributes objectForKey:RWTUtteranceAttributesKeyUtteranceProperties];

         AVSpeechUtterance *utterance = [[AVSpeechUtterance alloc] initWithString:utteranceString];

       [utterance setValuesForKeysWithDictionary:utteranceProperties];

         if (utterance) {

         [utterances addObject:utterance];

         [displayText appendString:utteranceString];

       }

     }

       page.displayText = displayText;

     page.backgroundImage = [UIImage imageNamed:[attributes objectForKey:RWTPageAttributesKeyBackgroundImage]];

     // 2

     page.utterances  = [utterances copy];

   }

     return page;

}

新加的代碼位於注釋 1 和注釋 2 處。它們根據NSString 或NSArray的情況來設定 page.utterances 屬性。

開啟 RWTPageViewController.h ,修改為如下內容:

#import <UIKit/UIKit.h>

@import AVFoundation;  

// 1

@interface RWTPageViewController : UIViewController<AVSpeechSynthesizerDelegate>  

@property (nonatomic, weak) IBOutlet UILabel *pageTextLabel;

@property (nonatomic, weak) IBOutlet UIImageView *pageImageView;  

@end

在注釋 1 處,聲明了RWTPageViewController 實現了AVSpeechSynthesizerDelegate 協議。

開啟 RWTPageViewController.m 加入:

  @property (nonatomic, assign) NSUInteger nextSpeechIndex;

我們用一個屬性來記錄下一個要讀到的 RWTPage.utterances 索引。

修改 setupForCurrentPage方法:

- (void)setupForCurrentPage {

   self.pageTextLabel.text = [self currentPage].displayText;

   self.pageImageView.image = [self currentPage].backgroundImage;

   self.nextSpeechIndex = 0;

}

修改 speakNextUtterance方法:

- (void)speakNextUtterance {

   // 1

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

     // 2

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

     self.nextSpeechIndex    += 1;

       // 3

     [self.synthesizer speakUtterance:utterance];

   }

}

  1. 在注釋為 1 的地方,我們先確保 nextSpeechUtterance 不會出現下標越界。
  2. 在注釋為 2 的地方,我們獲得當前要讀到的 utterance,然後 nextSpeechUtterance 加1。
  3. 最終,注釋為 3 的地方,朗讀 utterance。

編譯運行,聽到了嗎?在念第一頁的時候,你只聽見一個詞“Whisky”。因為我們還沒有實現其他的委託方法,因此當一個utterance 念完後,並不會接著念下一個 utterance。

修改 startSpeaking方法:

- (void)startSpeaking {

   if (!self.synthesizer) {

     self.synthesizer = [[AVSpeechSynthesizer alloc] init];

     // 1

     self.synthesizer.delegate = self;

   }

     [self speakNextUtterance];

}

注釋“1”的地方,我們將 ViewController 作為合成器的委託。

在 RWTPageViewController.m 最後加入一個方法:

 #pragma mark - AVSpeechSynthesizerDelegate Protocol   - (void)speechSynthesizer:(AVSpeechSynthesizer*)synthesizer didFinishSpeechUtterance:(AVSpeechUtterance*)utterance {

   NSUInteger indexOfUtterance = [[self currentPage].utterances indexOfObject:utterance];

   if (indexOfUtterance == NSNotFound) {

     return;

   }

     [self speakNextUtterance];

}

這段代碼在當前 utterance (文本)念完後,開始念下一段。

編譯運行,現在的效果是:

  • 一旦當前文本念完,自動跳到下一文本。直至這一頁都念完。
  • 無論向前還是向後翻頁時,這一頁的內容都不再繼續念了。
  • 發聲更自然,這歸功於 WhirlySquirrelly.plist檔案中的 utteranceProperties 欄位。為此,作者不得不手工調整每一個詞句。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.