O'Reilly logo

iOS 9 Swift Programming Cookbook by Vandad Nahavandipoor

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required