WAYNE PIEKARSKI: Hi.
Did you know that developers can now
add Android Auto support to their applications?
I'm Wayne Piekarski from Google, and in this Devbyte,
I'm going to show you how easy it
is to modify your existing audio application to work
with the new media APIs that'll make it work with Android Auto.
Your application, with its audio content, will run on the phone,
but it'll be heard and controlled
by the sound system built into your car.
The Android Auto software runs on your phone
and is responsible for drawing the user interface that
appears on the vehicle's display.
The phone controls the vehicle display via a USB cable.
And you should check out the Introduction to Android Auto
Devbyte to find out more about how this works.
I'm going to show you how to extend your existing music
application to work with Android Auto.
Let's say you've got a music playing
app that runs on the phone that contains a library of music.
You would want your users to be able to select and play music
from the car controls without having to use their phone,
even though the phone is where everything is processed.
Your app needs to tell Android Auto about this music library
so that it can present the media information
onto the screen along with the album artwork.
And your app needs to be able, also,
to respond to controls on the steering wheel,
such as Play, Stop, Next Track, and so forth.
Let me show you a quick demo of how
this looks like from the user's perspective.
What we're going to do now is launch
the music app, which then allows us to pick it from a playlist,
go to the genre, and then we can pick the song
from one of these categories here.
[MUSIC PLAYING]
Let's head back to the studio, and let's look at some code
so I can show you how it's done.
To implement all this cool functionality,
the first step is we need to set up the AndroidManifest.xml
to tell Android Auto that your application provides services
for cars.
You need to provide an XML file like this one
here with an automotive app tag, along with a media attribute
since we're implementing a music playing application.
You also need to declare a media browser service, which
tells Android Auto that your application is able to expose
information about available media.
Next, you need to implement the media browser service.
First, you implement the onGetRoot method,
which returns back the top level node of the media hierarchy.
And it's not shown in the UI.
This is where you should also implement
checks to prevent anything except Android Auto
from querying your application by checking
that the incoming client package name matches the Android Auto
package name.
The next step is to provide onLoadChildren,
which generates a list of media items, given the media ID
of a parent node.
Each item returned can be either browsable, like subfolders,
or playable, like a song.
And you can indicate the difference
by passing one of these flags in the Media Items Constructor.
Inside onLoadChildren, you should spawn off an async task,
and then call the detach method.
When the result is ready, the async task
can return back the results using send result.
Using an async task here ensures that the user interface
is responsive and is not blocked while your code is
querying your media library.
Now, since the user will be in a car,
it's important that you present items
in a way that minimizes user interaction.
So you should put frequently used items
like Playlist, Channels, and other popular content
at the top.
And you should avoid showing unbounded broad categories
like All Artists, because these lists will
be too hard for users to navigate through easily.
Instead, you should try to create bounded content that
is going to be what the user wants.
Next, we need to create a media session.
This acts as the glue that allows Android Auto to tell
our music application what media to play.
The media session is aware of our apps playback
status, the queue, and other metadata.
We then need to tie this media session to the media browser
service by using the setSessionToken method.
And make sure that you call setActive here as well,
because it indicates that the session is ready to receive
commands, and it's very important.
With the SessionToken, Android Auto
is now able to instantiate a media controller that
can remotely control our media session via the media session
callback methods.
So here's a diagram that shows a bit more about how everything
fits together.
You can see your media app interacting with the Android
Auto app on the phone, and how this then appears in the car.
This interface is not specific to Android Auto, by the way.
Android Auto is actually just a client
that uses the media controller.
And the big win here is that any other application for which you
give access to your SessionToken can also control the same media
session by creating a new media controller.
For example, your normal audio playing activity for the phone
should also be one of these clients,
delegating the media library handling and media playback
control to the same media browser service used for Auto.
To test your application, we have provided a media browser
simulator that looks similar to the car experience.
So you can run the simulator on your phone or the Android
Emulator to test that you're using the API correctly.
It uses the same media controller interface and media
browser service as Android Auto to present similar controls
as to what you'd experience in a car.
So this is great for testing at your desk during development.
All right.
So that's it.
So let's get back to the car and get out of here.
You can find out more about Android Auto by visiting
the documentation site and also joining the Google+ community
where you can share your ideas with everybody else.
Thanks very much for watching this intro video
to Android Auto.
I'm Wayne Piekarski, and I'll see you next time.