How to override focus engine update sound in tvOS

227 Views Asked by At

I am trying to override the sound that plays when a focus change is made on tvOS, but I cannot seem to find anything indicating if this is possible. I have looked through the Apple documentation a bit, and looked at some of the sound API's but none seemed to fit. Does anybody know if this is even possible? If this is possible how can this be achieved?

1

There are 1 best solutions below

0
On

This can be achieved with soundIdentifierForFocusUpdate, which was added to the SDK in tvOS 11

Using this method, you can customize or remove the default sound of tvOS played on focus updates.

To remove the sound you can return UIFocusSoundIdentifier.none

override func soundIdentifierForFocusUpdate(in context: UIFocusUpdateContext) -> UIFocusSoundIdentifier? {    
    return UIFocusSoundIdentifier.none
}

To use a different sound insted, you must include the new sound file in your target, and to load as shown here below:

let myPing = UIFocusSoundIdentifier.init(rawValue: "customPing")
let soundURL = Bundle.main.url(forResource: "ping", withExtension: "aif")!
UIFocusSystem.register(_: soundURL, forSoundIdentifier: myPing)

Then you have to return that sound new from soundIdentifierForFocusUpdate:

override func soundIdentifierForFocusUpdate(in context: UIFocusUpdateContext) -> UIFocusSoundIdentifier? {    
    return myPing
}

Everything is documented by Apple in the following link: Using Custom Sounds for Focus Movement