SlideShare a Scribd company logo
AVSpeechSynthesizerとロケール
@TachibanaKaoru
2013/11/13
自己紹介
• @TachibanaKaoru
• http://www.toyship.org/
• 渋谷の genesix で働く iOSエンジニア。
• 個人的にはこんなアプリを作ったりしています。

Toy Jigsaw

• Cookpad歴 11年 ( http://cookpad.com/kitchen/18627)
AVSpeechSynthesizerとは
• iOS7から導入されたAPIが公開された音声読み上げ機能。
• 文字列を与えると読み上げをしてくれます。
• delegateを使うと、「今読んでいる単語」なども取り
出せます。
• 詳しい使い方はこちら。
• https://github.com/toyship/StoryTeller
• http://www.toyship.org/archives/1483
ロケールの設定
• 文章を正しく読ませるためには、AVSpeechSynthesizerに読み上げる文の言語
に対応したロケール(NSLocale)を設定しないと正しく読み上げてくれません。
• AVSpeechSynthesizerが文章の言語を自動判別してくれるわけではありませ
ん。
• 今のところ、36個のロケールに対応しています。
!
!
!
!
!
!
!
!
!

AVSpeechSynthesizer* mySpeechSynthesizer = [[AVSpeechSynthesizer alloc] init];
mySpeechSynthesizer.delegate = self;
NSString* targetText = @"Hello World!";
AVSpeechUtterance *utterance = [AVSpeechUtterance speechUtteranceWithString:targetText];
AVSpeechSynthesisVoice* englishVoice = [AVSpeechSynthesisVoice voiceWithLanguage:@"en-US"];
utterance.voice = englishVoice;
[myspeechSynthesizer speakUtterance:utterance];
でも言語判定めんどくさいよね……。
Web APIは有料だし……。
システムの挙動は……
• iOSの純正アプリ(メールなど)では選択した文章
に応じて、ちゃんと適切に言語を判断。
• 例えばiPhoneのシステムロケールを日本語にした
状態でも、英語の文章を選択すると英語の声で読
んでくれます。
探したらありました、NSStringの言語判定
• CFStringTokenizerCopyBestStringLanguageを利用

!
! CFStringRef text =
(CFStringRef)CFBridgingRetain(self.inputText.text);
! CFRange range = CFRangeMake(0, CFStringGetLength(text));
! NSString *language =
(NSString*)CFBridgingRelease(CFStringTokenizerCopyBestStringLanguage(text, range));
CFStringTokenizerCopyBestStringLanguage仕様
• 指定した文字列の言語コードを返す。判別にはおよそ200から400文字が必
要。
• 30言語を判別可能。(アラビア語、ブルガリア語、チェコ語、デンマーク
語、ドイツ語、ギリシャ語、英語、スペイン語、フィンランド語、フランス
語、ヘブライ語、クロアチア語、ハンガリー語、アイスランド語、イタリア
語、日本語、韓国語、ノルウェー語、オランダ語、ポーランド語、ポルトガ
ル語、ルーマニア語、ロシア語、スロバキア語、スウェーデン語、タイ語、
トルコ語、ウクライナ語、簡体中国語、繁体中国語)
• でも実際の判別はちょっと微妙かも……。
AVSpeechSynthesizer Tips

• AVSpeechSynthesizerを使うときには、
CFStringTokenizerCopyBestStringLanguageを使って文
章の言語判定をしましょう!
おまけその1
• なぜ、AVSpeechSynthesizerに指定するのは言語コードではなくて、ロケール
のなんでしょうか。言語だけ指定すれば読んでくれるんじゃない?という気
もしますが……。
• ヒント:「12/3/2012」をen-GB(イギリス英語)とen-US(アメリカ英
語)で読ませてみてください。
おまけその2
• AVSpeechSynthesizerに設定する「ロケール」ですが、iPhoneでは、システム
言語とシステムロケールとは別々に設定できますよね。
• 例えばシステムの言語は日本語だけど、ロケールはen-GBになっていた場合は
AVSpeechSynthesizerのロケールはどうなるでしょうか。
• ja-JPになります。
• システム言語とシステムロケールを言語を優先してAVSpeechSynthesizer
のデフォルトロケールが決定されるようです。([NSLocale currentLocale]
がAVSpeechSynthesizerのデフォルトロケールではないことに注意。)

More Related Content

PPTX
15分でできるAmazon Alexa Skill開発
PDF
Downalodable Storyboard
PDF
Bluetooth LEとiBeaconを使った、すれ違い通信
PDF
アプリのバックグラウンド処理 | iOS 7エンジニア勉強会
PDF
Time for Xcode Behavior
PDF
Notifications in iOS10
PDF
Universal Link
PDF
Can we live in a pure Swift world?
15分でできるAmazon Alexa Skill開発
Downalodable Storyboard
Bluetooth LEとiBeaconを使った、すれ違い通信
アプリのバックグラウンド処理 | iOS 7エンジニア勉強会
Time for Xcode Behavior
Notifications in iOS10
Universal Link
Can we live in a pure Swift world?

More from toyship (11)

PDF
Swift Protocol and Selector
PDF
What's new Swift3
PDF
Xcode7時代のアプリ配布
PDF
My first tvOS
PDF
3D touch for iOS
PDF
Contents blocker on iOS9
PDF
Embedded framework and so on
PDF
はじめてのWKInterfaceController
PDF
App extension for iOS
PDF
サーバーからiOSアプリを変更する
PDF
Xcode bot
Swift Protocol and Selector
What's new Swift3
Xcode7時代のアプリ配布
My first tvOS
3D touch for iOS
Contents blocker on iOS9
Embedded framework and so on
はじめてのWKInterfaceController
App extension for iOS
サーバーからiOSアプリを変更する
Xcode bot
Ad

AVSpeechSynthesizerとロケール