[MUSIC PLAYING]
JOSH GORDON: Although Voice Actions are already
available on many different Android devices,
there's nowhere where they're more important than in the car.
Already you can reply to messages simply by speaking,
and now you can listen to music and other audio content
simply by saying what you want to hear.
Let's see, or rather hear, an example
of how this works on our reference application.
Listen to "Awakening" on Universal Media Player.
FEMALE SPEAKER: Playing "Awakening"
on Universal Media Player.
[MUSIC PLAYING]
JOSH GORDON: How cool is that?
I'm Josh Gordon from Google, and today I'll
show you how easy it is to add Voice
Actions to your existing Android auto applications.
This is important to do, because it
will help users stay focused on the road
as well as enjoy the ride.
Let's head back to the studio and see how it's done.
Voice Actions can be used to start and control playback.
Users initiate a Voice Action by tapping the mic button
on the car's display or by pressing
the corresponding steering wheel control.
You don't say "OK Google" as you would on a mobile device.
You can start playback in a number of different ways,
but they all take the form of Play Query on App.
Of course, you don't need to say the app
name if it's already running.
If you'd like to hear music of certain genre, you can say,
"play jazz on app," or you can say, listen "listen to rock."
You can also request to hear music
by artist, album, song, radio station and even by playlist.
You should support all of these that make sense for your app.
In addition to starting playback,
Voice Actions can also be used to control it.
When content is playing, users can press the steering wheel
button and say commands like "pause music" or "next song."
And here's something great for you.
If your app isn't already in the foreground when
a Voice Action comes in, Android Auto
will launch it automatically.
This means that Voice Actions give you
a way to take users directly from the home screen
into your content.
Now let's see how to implement Voice Actions in your existing
audio app.
First, if you'd like a refresher on how to build an audio app,
check out this great video by Wayne Piekarski.
The first step is to add the Media Play from search entry
to your manifest.
This enables Auto to launch your app in response
to "playback voice action."
Next, implement the On Play From Search method inside your media
session callback.
When the user finishes speaking, Auto
delivers the speech recognition results to this method.
In your implementation, you should
search your media library for matching content,
build your media sessions audio queue, and then start playback.
There are two parameters to this method,
and to understand them, let's say the user says,
"play Miles Davis" on your app.
The first parameter contains the search query,
in this case the string "Miles Davis."
The extras contain additional information Google provides.
In this case Google detects that Miles Davis is an artist
and adds an Artist Extra to the bundle.
These are the different extras provided by Android Auto,
and when they're available you can
use them to do a structured query of your database,
as opposed to having to search all fields.
They won't always be available though,
so you should also implement Logic to search
based on just the query string.
Here's an example of how you can retrieve the extras,
and you can find a complete code example
in the link at the end of this video.
Also be sure to check for an empty query string.
This can happen when the user doesn't specify any search
terms.
For example, if they just say, "play anything."
Handle this case by playing what makes most sense for your app.
For example, a recent or favorite song.
Next, implement Search Logic to find content
that matches the user's query.
If your search doesn't return immediately,
set the playback state to "state connecting,"
and perform your search on an async thread.
Build your media sessions Audio Queue with the search results,
keeping in mind there could be more than one.
For example, if the user searches for an album,
add all its songs to the queue so the next song
starts playing automatically after the current song
finishes.
And when search results are ready,
be sure to start playback without requiring the driver
to take any other action.
On the other hand, if no search results were found,
stop playback and show an error.
Now it's time to enable Playback Controls.
When the user says a command, Android Auto
triggers the corresponding method in your Media Session
Callback.
There are just two steps.
First, set these flags on your Media Session Object.
Now you only have to implement the corresponding callback
methods.
For complete code example of how both types of Voice Actions
work, check out the Universal Media Player Reference
app, and you can see our developer training
for much more info.
Thanks very much for joining me, and see you next time.
[MUSIC PLAYING]