SFSpeechRecognizer

Struct SFSpeechRecognizer 

Source
pub struct SFSpeechRecognizer { /* private fields */ }
Available on crate feature SFSpeechRecognizer only.
Expand description

An object you use to check for the availability of the speech recognition service, and to initiate the speech recognition process.

An SFSpeechRecognizer object is the central object for managing the speech recognizer process. Use this object to:

  • Request authorization to use speech recognition services.
  • Specify the language to use during the recognition process.
  • Initiate new speech recognition tasks.

§Set up speech recognition

Each speech recognizer supports only one language, which you specify at creation time. The successful creation of a speech recognizer does not guarantee that speech recognition services are available. For some languages, the recognizer might require an Internet connection. Use the isAvailable property to find out if speech recognition services are available for the current language.

To initiate the speech recognition process, do the following:

  1. Request authorization to use speech recognition. See .
  2. Create an SFSpeechRecognizer object.
  3. Verify the availability of services using the isAvailable property of your speech recognizer object.
  4. Prepare your audio content.
  5. Create a recognition request object—an object that descends from SFSpeechRecognitionRequest.
  6. Call the recognitionTask(with:delegate:) or recognitionTask(with:resultHandler:) method to begin the recognition process.

The type of recognition request object you create depends on whether you are processing an existing audio file or an incoming stream of audio. For existing audio files, create a SFSpeechURLRecognitionRequest object. For audio streams, create a SFSpeechAudioBufferRecognitionRequest object.

§Create a great user experience for speech recognition

Here are some tips to consider when adding speech recognition support to your app.

  • Be prepared to handle failures caused by speech recognition limits. Because speech recognition is a network-based service, limits are enforced so that the service can remain freely available to all apps. Individual devices may be limited in the number of recognitions that can be performed per day, and each app may be throttled globally based on the number of requests it makes per day. If a recognition request fails quickly (within a second or two of starting), check to see if the recognition service became unavailable. If it is, you may want to ask users to try again later.
  • Plan for a one-minute limit on audio duration. Speech recognition places a relatively high burden on battery life and network usage. To minimize this burden, the framework stops speech recognition tasks that last longer than one minute. This limit is similar to the one for keyboard-related dictation.
  • Remind the user when your app is recording. For example, display a visual indicator and play sounds at the beginning and end of speech recognition to help users understand that they’re being actively recorded. You can also display speech as it is being recognized so that users understand what your app is doing and see any mistakes made during the recognition process.
  • Do not perform speech recognition on private or sensitive information. Some speech is not appropriate for recognition. Don’t send passwords, health or financial data, and other sensitive speech for recognition.

See also Apple’s documentation

Implementations§

Source§

impl SFSpeechRecognizer

Source

pub unsafe fn supportedLocales() -> Retained<NSSet<NSLocale>>

Returns the set of locales that are supported by the speech recognizer.

This method returns the locales for which speech recognition is supported. Support for a locale does not guarantee that speech recognition is currently possible for that locale. For some locales, the speech recognizer requires an active Internet connection to communicate with Apple’s servers. If the speech recognizer is currently unable to process requests, isAvailable returns false.

Speech recognition supports the same locales that are supported by the keyboard’s dictation feature. For a list of these locales, see QuickType Keyboard: Dictation.

  • Returns: A set of locales that support speech recognition.
Source

pub unsafe fn authorizationStatus() -> SFSpeechRecognizerAuthorizationStatus

Returns your app’s current authorization to perform speech recognition.

The user can reject your app’s request to perform speech recognition, but your request can also be denied if speech recognition is not supported on the device. The app can also change your app’s authorization status at any time from the Settings app.

  • Returns: The app’s current authorization status value. For a list of values, see SFSpeechRecognizerAuthorizationStatus.
Source

pub unsafe fn requestAuthorization( handler: &DynBlock<dyn Fn(SFSpeechRecognizerAuthorizationStatus)>, )

Available on crate feature block2 only.

Asks the user to allow your app to perform speech recognition.

Call this method before performing any other tasks associated with speech recognition. This method executes asynchronously, returning shortly after you call it. At some point later, the system calls the provided handler block with the results.

When your app’s authorization status is SFSpeechRecognizerAuthorizationStatus/notDetermined, this method causes the system to prompt the user to grant or deny permission for your app to use speech recognition. The prompt includes the custom message you specify in the NSSpeechRecognitionUsageDescription key of your app’s Info.plist file. The user’s response is saved so that future calls to this method do not prompt the user again.

Important: Your app’s Info.plist file must contain the NSSpeechRecognitionUsageDescription key with a valid usage description. If this key is not present, your app will crash when you call this method.

For more information about requesting authorization, see .

  • Parameters:
  • handler: The block to execute when your app’s authorization status is known. The status parameter of the block contains your app’s authorization status. The system does not guarantee the execution of this block on your app’s main dispatch queue.
Source

pub unsafe fn init(this: Allocated<Self>) -> Option<Retained<Self>>

Creates a speech recognizer associated with the user’s default language settings.

If the user’s default language is not supported for speech recognition, this method attempts to fall back to the language used by the keyboard for dictation. If that fails, this method returns nil.

Even if this method returns a valid speech recognizer object, the speech recognition services may be temporarily unavailable. To determine whether speech recognition services are available, check the isAvailable property.

  • Returns: An initialized speech recognizer object, or nil if there was a problem creating the object.
Source

pub unsafe fn initWithLocale( this: Allocated<Self>, locale: &NSLocale, ) -> Option<Retained<Self>>

Creates a speech recognizer associated with the specified locale.

If you specify a language that is not supported by the speech recognizer, this method attempts to fall back to the language used by the keyboard for dictation. If that fails, this method returns nil.

Even if this method returns a valid speech recognizer object, the speech recognition services may be temporarily unavailable. To determine whether speech recognition services are available, check the isAvailable property.

  • Parameters:

  • locale: The locale object representing the language you want to use for speech recognition. For a list of languages supported by the speech recognizer, see supportedLocales().

  • Returns: An initialized speech recognizer object, or nil if the specified language was not supported.

Source

pub unsafe fn isAvailable(&self) -> bool

A Boolean value that indicates whether the speech recognizer is currently available.

When the value of this property is true, you may create new speech recognition tasks. When value of this property is false, speech recognition services are not available.

Source

pub unsafe fn locale(&self) -> Retained<NSLocale>

The locale of the speech recognizer.

The locale of the speech recognizer is an NSLocale object. The default value of this property is the system locale (that is, +[NSLocale systemLocale]).

Source

pub unsafe fn supportsOnDeviceRecognition(&self) -> bool

A Boolean value that indicates whether the speech recognizer can operate without network access.

An SFSpeechRecognitionRequest can only honor its SFSpeechRecognitionRequest/requiresOnDeviceRecognition property if supportsOnDeviceRecognition is true. If supportsOnDeviceRecognition is false, the SFSpeechRecognizer requires a network in order to recognize speech.

Source

pub unsafe fn setSupportsOnDeviceRecognition( &self, supports_on_device_recognition: bool, )

Source

pub unsafe fn delegate( &self, ) -> Option<Retained<ProtocolObject<dyn SFSpeechRecognizerDelegate>>>

The delegate object that handles changes to the availability of speech recognition services.

Provide a delegate object when you want to monitor changes to the availability of speech recognition services. Your delegate object must conform to the SFSpeechRecognizerDelegate protocol.

Source

pub unsafe fn setDelegate( &self, delegate: Option<&ProtocolObject<dyn SFSpeechRecognizerDelegate>>, )

Setter for delegate.

This is a weak property.

Source

pub unsafe fn defaultTaskHint(&self) -> SFSpeechRecognitionTaskHint

Available on crate feature SFSpeechRecognitionTaskHint only.

A hint that indicates the type of speech recognition being requested.

By default, the value of this property overrides the SFSpeechRecognitionTaskHint/unspecified value for requests. For possible values, see SFSpeechRecognitionTaskHint.

Source

pub unsafe fn setDefaultTaskHint( &self, default_task_hint: SFSpeechRecognitionTaskHint, )

Available on crate feature SFSpeechRecognitionTaskHint only.

Setter for defaultTaskHint.

Source

pub unsafe fn recognitionTaskWithRequest_resultHandler( &self, request: &SFSpeechRecognitionRequest, result_handler: &DynBlock<dyn Fn(*mut SFSpeechRecognitionResult, *mut NSError)>, ) -> Retained<SFSpeechRecognitionTask>

Available on crate features SFSpeechRecognitionRequest and SFSpeechRecognitionResult and SFSpeechRecognitionTask and block2 only.

Executes the speech recognition request and delivers the results to the specified handler block.

Use this method to initiate the speech recognition process on the audio contained in the request object. This method executes asynchronously and returns a SFSpeechRecognitionTask object that you can use to cancel or finalize the recognition process later. As results become available, the method calls the block in the resultHandler parameter.

  • Parameters:

  • request: A request (in an SFSpeechRecognitionRequest object) to recognize speech from an audio source.

  • resultHandler: The block to call when partial or final results are available, or when an error occurs. If the SFSpeechRecognitionRequest/shouldReportPartialResults property is true, this block may be called multiple times to deliver the partial and final results. The block has no return value and takes the following parameters:

  • term result: A SFSpeechRecognitionResult containing the partial or final transcriptions of the audio content.

  • term error: An error object if a problem occurred. This parameter is nil if speech recognition was successful.

  • Returns: The task object you can use to manage an in-progress recognition request.

Source

pub unsafe fn recognitionTaskWithRequest_delegate( &self, request: &SFSpeechRecognitionRequest, delegate: &ProtocolObject<dyn SFSpeechRecognitionTaskDelegate>, ) -> Retained<SFSpeechRecognitionTask>

Available on crate features SFSpeechRecognitionRequest and SFSpeechRecognitionTask only.

Recognizes speech from the audio source associated with the specified request, using the specified delegate to manage the results.

Use this method to initiate the speech recognition process on the audio contained in the request object. This method executes asynchronously and returns a SFSpeechRecognitionTask object that you can use to cancel or finalize the recognition process later. As results become available, the method calls the methods of the provided delegate object.

Note that the SFSpeechRecognitionTask object returned by this method does not retain your delegate object. You must maintain a strong reference to your delegate while speech recognition is in progress.

  • Parameters:

  • request: A request (encapsulated in an SFSpeechRecognitionRequest object) to recognize speech from an audio source.

  • delegate: An object that can handle results from the speech recognition task. This object must conform to the SFSpeechRecognitionTaskDelegate protocol.

  • Returns: The task object you can use to manage an in-progress recognition request.

Source

pub unsafe fn queue(&self) -> Retained<NSOperationQueue>

The queue on which to execute recognition task handlers and delegate methods.

The default value of this property is the app’s main queue. Assign a different queue if you want delegate methods and handlers to be executed on a background queue.

The handler you pass to the requestAuthorization(_:) method does not use this queue.

Source

pub unsafe fn setQueue(&self, queue: &NSOperationQueue)

Setter for queue.

§Safety

queue possibly has additional threading requirements.

Source§

impl SFSpeechRecognizer

Methods declared on superclass NSObject.

Source

pub unsafe fn new() -> Retained<Self>

Methods from Deref<Target = NSObject>§

Source

pub fn doesNotRecognizeSelector(&self, sel: Sel) -> !

Handle messages the object doesn’t recognize.

See Apple’s documentation for details.

Methods from Deref<Target = AnyObject>§

Source

pub fn class(&self) -> &'static AnyClass

Dynamically find the class of this object.

§Panics

May panic if the object is invalid (which may be the case for objects returned from unavailable init/new methods).

§Example

Check that an instance of NSObject has the precise class NSObject.

use objc2::ClassType;
use objc2::runtime::NSObject;

let obj = NSObject::new();
assert_eq!(obj.class(), NSObject::class());
Source

pub unsafe fn get_ivar<T>(&self, name: &str) -> &T
where T: Encode,

👎Deprecated: this is difficult to use correctly, use Ivar::load instead.

Use Ivar::load instead.

§Safety

The object must have an instance variable with the given name, and it must be of type T.

See Ivar::load_ptr for details surrounding this.

Source

pub fn downcast_ref<T>(&self) -> Option<&T>
where T: DowncastTarget,

Attempt to downcast the object to a class of type T.

This is the reference-variant. Use Retained::downcast if you want to convert a retained object to another type.

§Mutable classes

Some classes have immutable and mutable variants, such as NSString and NSMutableString.

When some Objective-C API signature says it gives you an immutable class, it generally expects you to not mutate that, even though it may technically be mutable “under the hood”.

So using this method to convert a NSString to a NSMutableString, while not unsound, is generally frowned upon unless you created the string yourself, or the API explicitly documents the string to be mutable.

See Apple’s documentation on mutability and on isKindOfClass: for more details.

§Generic classes

Objective-C generics are called “lightweight generics”, and that’s because they aren’t exposed in the runtime. This makes it impossible to safely downcast to generic collections, so this is disallowed by this method.

You can, however, safely downcast to generic collections where all the type-parameters are AnyObject.

§Panics

This works internally by calling isKindOfClass:. That means that the object must have the instance method of that name, and an exception will be thrown (if CoreFoundation is linked) or the process will abort if that is not the case. In the vast majority of cases, you don’t need to worry about this, since both root objects NSObject and NSProxy implement this method.

§Examples

Cast an NSString back and forth from NSObject.

use objc2::rc::Retained;
use objc2_foundation::{NSObject, NSString};

let obj: Retained<NSObject> = NSString::new().into_super();
let string = obj.downcast_ref::<NSString>().unwrap();
// Or with `downcast`, if we do not need the object afterwards
let string = obj.downcast::<NSString>().unwrap();

Try (and fail) to cast an NSObject to an NSString.

use objc2_foundation::{NSObject, NSString};

let obj = NSObject::new();
assert!(obj.downcast_ref::<NSString>().is_none());

Try to cast to an array of strings.

use objc2_foundation::{NSArray, NSObject, NSString};

let arr = NSArray::from_retained_slice(&[NSObject::new()]);
// This is invalid and doesn't type check.
let arr = arr.downcast_ref::<NSArray<NSString>>();

This fails to compile, since it would require enumerating over the array to ensure that each element is of the desired type, which is a performance pitfall.

Downcast when processing each element instead.

use objc2_foundation::{NSArray, NSObject, NSString};

let arr = NSArray::from_retained_slice(&[NSObject::new()]);

for elem in arr {
    if let Some(data) = elem.downcast_ref::<NSString>() {
        // handle `data`
    }
}

Trait Implementations§

Source§

impl AsRef<AnyObject> for SFSpeechRecognizer

Source§

fn as_ref(&self) -> &AnyObject

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<NSObject> for SFSpeechRecognizer

Source§

fn as_ref(&self) -> &NSObject

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl AsRef<SFSpeechRecognizer> for SFSpeechRecognizer

Source§

fn as_ref(&self) -> &Self

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl Borrow<AnyObject> for SFSpeechRecognizer

Source§

fn borrow(&self) -> &AnyObject

Immutably borrows from an owned value. Read more
Source§

impl Borrow<NSObject> for SFSpeechRecognizer

Source§

fn borrow(&self) -> &NSObject

Immutably borrows from an owned value. Read more
Source§

impl ClassType for SFSpeechRecognizer

Source§

const NAME: &'static str = "SFSpeechRecognizer"

The name of the Objective-C class that this type represents. Read more
Source§

type Super = NSObject

The superclass of this class. Read more
Source§

type ThreadKind = <<SFSpeechRecognizer as ClassType>::Super as ClassType>::ThreadKind

Whether the type can be used from any thread, or from only the main thread. Read more
Source§

fn class() -> &'static AnyClass

Get a reference to the Objective-C class that this type represents. Read more
Source§

fn as_super(&self) -> &Self::Super

Get an immutable reference to the superclass.
Source§

impl Debug for SFSpeechRecognizer

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for SFSpeechRecognizer

Source§

type Target = NSObject

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl Hash for SFSpeechRecognizer

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Message for SFSpeechRecognizer

Source§

fn retain(&self) -> Retained<Self>
where Self: Sized,

Increment the reference count of the receiver. Read more
Source§

impl NSObjectProtocol for SFSpeechRecognizer

Source§

fn isEqual(&self, other: Option<&AnyObject>) -> bool
where Self: Sized + Message,

Check whether the object is equal to an arbitrary other object. Read more
Source§

fn hash(&self) -> usize
where Self: Sized + Message,

An integer that can be used as a table address in a hash table structure. Read more
Source§

fn isKindOfClass(&self, cls: &AnyClass) -> bool
where Self: Sized + Message,

Check if the object is an instance of the class, or one of its subclasses. Read more
Source§

fn is_kind_of<T>(&self) -> bool
where T: ClassType, Self: Sized + Message,

👎Deprecated: use isKindOfClass directly, or cast your objects with AnyObject::downcast_ref
Check if the object is an instance of the class type, or one of its subclasses. Read more
Source§

fn isMemberOfClass(&self, cls: &AnyClass) -> bool
where Self: Sized + Message,

Check if the object is an instance of a specific class, without checking subclasses. Read more
Source§

fn respondsToSelector(&self, aSelector: Sel) -> bool
where Self: Sized + Message,

Check whether the object implements or inherits a method with the given selector. Read more
Source§

fn conformsToProtocol(&self, aProtocol: &AnyProtocol) -> bool
where Self: Sized + Message,

Check whether the object conforms to a given protocol. Read more
Source§

fn description(&self) -> Retained<NSObject>
where Self: Sized + Message,

A textual representation of the object. Read more
Source§

fn debugDescription(&self) -> Retained<NSObject>
where Self: Sized + Message,

A textual representation of the object to use when debugging. Read more
Source§

fn isProxy(&self) -> bool
where Self: Sized + Message,

Check whether the receiver is a subclass of the NSProxy root class instead of the usual NSObject. Read more
Source§

fn retainCount(&self) -> usize
where Self: Sized + Message,

The reference count of the object. Read more
Source§

impl PartialEq for SFSpeechRecognizer

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl RefEncode for SFSpeechRecognizer

Source§

const ENCODING_REF: Encoding = <NSObject as ::objc2::RefEncode>::ENCODING_REF

The Objective-C type-encoding for a reference of this type. Read more
Source§

impl DowncastTarget for SFSpeechRecognizer

Source§

impl Eq for SFSpeechRecognizer

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<'a, T> AnyThread for T
where T: ClassType<ThreadKind = dyn AnyThread + 'a> + ?Sized,

Source§

fn alloc() -> Allocated<Self>
where Self: Sized + ClassType,

Allocate a new instance of the class. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> AutoreleaseSafe for T
where T: ?Sized,