AVFoundation in Mojave (or Catalina)?

I'm a novice Swift coder. I'm working on a project that requires text-to-speech functionality on the Mac. It was my understanding the AVFoundation was working under macOS 10.14, but when I try to import and use it, Xcode claims to not recognize any of the classes or methods.


When I create a new iOS project, however, it works.


So, can anyone more knowledgeable than me (ie, nearly everybody) confirm whether AVFoundation is or is not working in Mojave?


Alternately, what might I be doing wrong? And will it be available in Catalina?

Answered by DTS Engineer in 375514022

Well, that’s interesting. To understand what’s going on here, you need to look at the C headers in the macOS SDK:

  1. The

    AVSpeechSynthesis.h
    header is present, with the path
    System/Library/Frameworks/AVFoundation.framework/Versions/A/Frameworks/AVFAudio.framework/Versions/A/Headers/AVSpeechSynthesis.h
    .
  2. The

    AVSpeechSynthesizer
    class is decorated as being available in 10.14 and later:
    NS_CLASS_AVAILABLE(10_14, 7_0)
    @interface AVSpeechSynthesizer : NSObject
    …
    @end

    In theory that means it should be available. However, it’s not, so we have to pop up a level.

  3. AVSpeechSynthesizer
    is part of the AVFAudio framework, which is a subframework of the AVFoundation framework. If you look at the AVFAudio’s main header,
    System/Library/Frameworks/AVFoundation.framework/Versions/A/Frameworks/AVFAudio.framework/Versions/A/Headers/AVFAudio.h
    , you’ll see this:
    #if TARGET_OS_IPHONE
    #import <AVFAudio/AVAudioSession.h>
    #import <AVFAudio/AVSpeechSynthesis.h>
    #endif

    D’oh!

As to what you should do about this, that depends on where you are with your product:

  • If you can reasonably assume that Xcode 11 will GM before your product, you can just switch to the beta. Keep in mind that, if you intend to deploy back to pre-10.14 systems, you’ll need an

    NSSpeechSynthesizer
    compatibility path.
  • If you plan to ship soon, you could just write the

    NSSpeechSynthesizer
    compatibility code and use that now, then add the
    AVSpeechSynthesis
    code path in once you’ve made the leap to Xcode 11.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

AVFoundation was introduced back in macOS 10.7, so it’s definitely available in 10.14. Also, AVFoundation is our current, preferred media API, so it’s not going away any time soon.

I’m not sure why it’s failing in your original project while working in a test project. Can you be more specific about the error you’re seeing? If you add just an

import AVFoundation
to your file, does that fail? Or does that work, and yet you still can’t access the AVFoundation API?

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Hallo, eskimo, thanks for the reply.

If I create a macOS project and simply "import AVFoundation", each time I try to work with any AVFoundation properties or methods I get an "Use of unresolved identifier" error.


For example:


import AVFoundation

let synthesizer = AVSpeechSynthesizer()


and line three gets "Use of unresolved identifier 'AVSpeechSynthesizer'."


if I do the same, but inside an iOS project, no error.


This is why I was asking if it actually was implemented or not, because I've seen one or two comments around the web to the effect that though it says it's available, it isn't really.

I tested in an existin,g project and it works.


Where do you insert


let synthesizer = AVSpeechSynthesizer()

Right after I import. The code snippet above was copied and pasted directly. But it doesn't matter - if I put it inside a function, the same error pops up.


Is there something wrong in how I'm setting up the project? I just choose New Project -> macOS -> Cocoa App. Then I add a new Swift file and start trying to code.

I created a new macOS - Cocoa App project and got the same error.

Despite the doc says AVSpeechSynthesizer is available in mac OS SDK 10.14+, I could not find any headers including AVSpeechSynthesizer in the macOS SDK 10.14 bundled with Xcode 10.3.


Maybe you can work with NSSpeechSynthesizer.

Well, at least I know I'm not crazy. (In this particular instance, anyway.)


Thanks for the suggestion, I'll give NSSpeechSynthesizer a look.

I'm not sure what's going on here, but it's possible that AVSpeechSynthesizer was made public in the 10.15 SDK, yet is available for use back to 10.14 because it was in there as private (or unannounced) API last year.


That would mean you have to use Xcode 11 (which is the first Xcode to have the 10.15 SDK) to use it in source code at all, but once you're in Xcode 11 you can use it in apps for 10.14 too.

Accepted Answer

Well, that’s interesting. To understand what’s going on here, you need to look at the C headers in the macOS SDK:

  1. The

    AVSpeechSynthesis.h
    header is present, with the path
    System/Library/Frameworks/AVFoundation.framework/Versions/A/Frameworks/AVFAudio.framework/Versions/A/Headers/AVSpeechSynthesis.h
    .
  2. The

    AVSpeechSynthesizer
    class is decorated as being available in 10.14 and later:
    NS_CLASS_AVAILABLE(10_14, 7_0)
    @interface AVSpeechSynthesizer : NSObject
    …
    @end

    In theory that means it should be available. However, it’s not, so we have to pop up a level.

  3. AVSpeechSynthesizer
    is part of the AVFAudio framework, which is a subframework of the AVFoundation framework. If you look at the AVFAudio’s main header,
    System/Library/Frameworks/AVFoundation.framework/Versions/A/Frameworks/AVFAudio.framework/Versions/A/Headers/AVFAudio.h
    , you’ll see this:
    #if TARGET_OS_IPHONE
    #import <AVFAudio/AVAudioSession.h>
    #import <AVFAudio/AVSpeechSynthesis.h>
    #endif

    D’oh!

As to what you should do about this, that depends on where you are with your product:

  • If you can reasonably assume that Xcode 11 will GM before your product, you can just switch to the beta. Keep in mind that, if you intend to deploy back to pre-10.14 systems, you’ll need an

    NSSpeechSynthesizer
    compatibility path.
  • If you plan to ship soon, you could just write the

    NSSpeechSynthesizer
    compatibility code and use that now, then add the
    AVSpeechSynthesis
    code path in once you’ve made the leap to Xcode 11.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Thanks again, eskimo! I'm glad to know the reason behind it.


I'll just work on another part of the app until the Xcode 11 GM.

AVFoundation in Mojave (or Catalina)?
 
 
Q