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?
Well, that’s interesting. To understand what’s going on here, you need to look at the C headers in the macOS SDK:
The
header is present, with the pathAVSpeechSynthesis.h
.System/Library/Frameworks/AVFoundation.framework/Versions/A/Frameworks/AVFAudio.framework/Versions/A/Headers/AVSpeechSynthesis.h
The
class is decorated as being available in 10.14 and later:AVSpeechSynthesizer
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.
is part of the AVFAudio framework, which is a subframework of the AVFoundation framework. If you look at the AVFAudio’s main header,AVSpeechSynthesizer
, you’ll see this:System/Library/Frameworks/AVFoundation.framework/Versions/A/Frameworks/AVFAudio.framework/Versions/A/Headers/AVFAudio.h
#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
compatibility path.NSSpeechSynthesizer
If you plan to ship soon, you could just write the
compatibility code and use that now, then add theNSSpeechSynthesizer
code path in once you’ve made the leap to Xcode 11.AVSpeechSynthesis
Share and Enjoy
—
Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware
let myEmail = "eskimo" + "1" + "@apple.com"