Chapter 12. 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.

12.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 12-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 12-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 to ...

Get iOS 9 Swift Programming Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.