Skip to Content
iOS 10 Swift Programming Cookbook
book

iOS 10 Swift Programming Cookbook

by Vandad Nahavandipoor
December 2016
Intermediate to advanced content levelIntermediate to advanced
456 pages
8h 46m
English
O'Reilly Media, Inc.
Content preview from iOS 10 Swift Programming Cookbook

Chapter 16. Multimedia

The current version of iOS brings some changes to multimedia playback and functionality, especially the AVFoundation framework. In this chapter, we will have a look at those additions and some of the changes.

Note

Make sure that you have imported the AVFoundation framework in your app before running the code in this chapter.

16.1 Reading Out Text with the Default Siri Alex Voice

Problem

You want to use the default Siri Alex voice on a device to speak some text.

Solution

Instantiate AVSpeechSynthesisVoice with the identifier initializer and pass the value of AVSpeechSynthesisVoiceIdentifierAlex to it.

Discussion

Let’s create an example out of this. Create your UI so that it looks like Figure 16-1. Place a text view on the screen and a bar button item in your navigation bar. When the button is pressed, you will ask Siri to speak out the text inside the text view.

Figure 16-1. Text view and button in the UI

I’ve linked the text view to a property in my view controller called textView:

  @IBOutlet var textView: UITextView!

When the read button is pressed, check first whether Alex is available:

guard let voice = AVSpeechSynthesisVoice(identifier:
  AVSpeechSynthesisVoiceIdentifierAlex) else{
    print("Alex is not available")
    return
}
            

Instances of AVSpeechSynthesisVoice have properties such as identifier, quality, and name. The identifier can be used later ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

iOS 11 Swift Programming Cookbook

iOS 11 Swift Programming Cookbook

Vandad Nahavandipoor
iOS 7 Programming Cookbook

iOS 7 Programming Cookbook

Vandad Nahavandipoor

Publisher Resources

ISBN: 9781491966426Errata PageSupplemental Content