Modify Focus Manager
Modify Alexa Focus Manager Library
Your device only has one speaker, but at any given time there might be multiple Capability Agents that wish to use it. The Focus Manager exists to ensure a consistent user experience and prevent the chaos of multiple agents speaking over each other at the same time. But how does it know what to give control of the speaker to? We've divided different functions (such as Speech, Alarms, and Music) into various Channels that take priority over each other in a structure determined by the Focus Manager.
What if your device's specific use case involved critical functions that you didn't want interrupted? For example, if you were building an Alexa-enabled navigation system, your users wouldn't want to miss Alexa's driving directions because an alarm went off and took control of your speaker. In this case, you'd want to put driving directions in the highest priority channel. Let's try an example interaction to learn about the behavior of your Sample App's Focus Manager.
Initiate two competing Capability Agents
Because Alexa lives in the cloud, all AVS products become more capable every day. As your customers start doing more with your device, the Focus Manager will come into play more frequently. Let's create a situation that will force the Focus Manager to take control of the Media Player according to the existing Channel Priority set in your Sample App.
- Ask "Alexa, set a timer for 20 seconds".
- You should receive a confirmation from Alexa - "Twenty seconds - starting now".
- Quickly, say "Alexa, sing me a song!"
- Within a few seconds, Alexa will start to sing you a pretty great song…
- … but before the song has a chance to finish - the alarm goes off! Unfortunately, your song stopped - it lost the Focus of the device.
- Say "Alexa, stop" to get the Alarm to quit beeping.
- Once your Alarms capability agent has stopped, your song will be allowed to retake the device's Foreground channel.
Imagine if that song was actually driving directions, about to instruct you to exit off the freeway - you might have just missed your turn. Let's modify our Focus Manager to increase the priority of the Content channel over the Alarms channel.
Modify your Focus Manager Interface
- In your File Manager, navigate to /home/pi/avs-device-sdk/AVSCommon/SDKInterfaces/include/AVSCommon/SDKInterfaces
- Right-click on FocusManagerInterface.h and select Text Editor to open the file.
- Scroll down and you should see the channel priorities listed under the
FocusManagerInterfaceclass. Lower numbers are higher priority. Notice how rather than 1st/2nd/3rd, they're listed as 100/200/300? This gives you the space to create entirely new channels and place them wherever you wish. For example, you could create a channel number for driving directions and give it a priority of 1-99 to even interrupt Dialog.
- For this exercise, we want to bump up the priority of Content so that it doesn't get stepped on by Alerts.
CONTENT_CHANNEL_PRIORITYis currently set to
300. Let's make it a higher priority than our 20-second Timer (which is in the Alerts Channel) by changing that
Once you've increased the priority of the Content Channel, save your FocusManagerInterface.h file and close it.
Rebuild your modified sample app
You'll need to rebuild the sample app for the changes to take effect. First, quit out of your existing instance of the Sample App (if it's still running) by closing the window, Ctrl-C'ing out, or the more elegant typing "q" and hitting return. Open a terminal and input the following command to rebuild the Sample App:
cd /home/pi/build/SampleApp sudo make
You'll see the modified libraries rebuilding - it might take a couple of minutes. When you get the message that your target SampleApp is 100% built, it's time to re-launch the Sample App and try the same interaction. Restart the Sample App by running this command in a terminal:
cd /home/pi sudo bash startsample.sh
Now, start a 20-second timer again and ask Alexa for another song. This time, your song should keep playing even when your timer goes off. If you were building Alexa-enabled smart speakers for DJs, they might appreciate your device never interrupting a song.
Feel free to experiment with other priorities in the Focus Manager to create the best user experience for your product's specific use case. This can help differentiate your product from every other Alexa-enabled device on the market. Keep in mind that when you ship your own commercial Alexa-enabled products, they may have different interaction models or capabilities, but your customers will still expect your device to listen and respond instantly no matter what else is happening.