As a developer advocate for media, Marc works with large media partners to help them integrate their streaming apps into the Android framework. He contributed to ExoPlayer extensions to make integration with the Android framework easy and reliable (UI, MediaSession, Notifications). Prior to Google, Marc was a Software Engineer and Engineering Consultant for two decades. He earned a degree in Sociology and Economic History at the University of Zürich.View the profile
Andrew works on ExoPlayer, an open source media playback library for Android. Previously he worked on automatic movie creation in the Google Photos app. He has a Ph.D. from the University of Cambridge, where he studied forensic signal processing in the Security Group.View the profile
About the talk
This session will guide developers through the process of building a feature-rich media application using ExoPlayer. It will demonstrate how ExoPlayer makes it easy for developers to add advanced functionality such as caching and offline to their apps. It will also show how ExoPlayer’s extension modules can be used to integrate with other Google APIs such as MediaSession, Cast, and Interactive Media Ads to create a seamless user experience.
Hi everyone. Thank you very much for coming alone. I'm on dream. I'm a software engineer on the XO player team and I'm Mark on developer advocate for Android media. Are you can build really really fully-featured media playback functionality into Apps using quiet little code using actually file locations. We're going to stop by focusing on the video you space we're going to build a simple video player app that will play an mp4 stream. Let me going to show you how to insert at so long for that video
content then we going to shift also because to order you are going to show you how to play audio in background service. We going to show you how to integrate with Android media sessions. And finally we're going to take a look at some new functionality review recently released for downloading media for offline playback. I like lost you but hoping to have a mixture of some introductory content. I don't sell you some coverage of slightly more tricky use cases. So hopefully there's something for everyone. I'm going to stop by. What exit is it is for anyone who hasn't used the library before.
Okay. Liquor doesn't seem to be working next light great is an application Level media player. That means that you choose which function of X. I thought you'd like to use and then you bundle the play cementation into application as a dependency assistant playback functionality different Android versions of different devices. Xrp fireworks from API 1600 Jelly Bean outfits that you can reach more than 99% of active Isis before we have an active community that you can
file issues with feature request bucks and you can even send this pool request. Exoplayer is also quite widely used by both within the Google applications like that you chew about phinedroids and also in Google Play movies. It's also quite widely used in the whydah Android Community. I never actually over 200,000 applications on the Play Store that are using X and the Teen Choice really excited to see the kind of products that your old building. Since last year's talk we've been working hard on face and stability and performance improvements behind the scenes. But also on some
brand new features and enduring that open into quite a fully-featured solution for media fight back. Hissed at some of the highlights of the features that we've added over the past year to go into detail on here, but I'd encourage you to check out the release notes and the root directory of what you can find Soul T cells all of the changes included in each new release of the plan. In today's talk which is going to cover a few highlights including support for downloading media as I mentioned notifications
in lifeguarding and to each of these two rap song video output audio up a starting point will be a mostly empty project because we want to show you as much of the interesting narrative possible way. You can check out the code off the roads. If you want to look at things in a bit more detail starting point is a is a nice Chianti project that I mentioned. We're going to make a video player app where we start playing the stream as soon as the top comes into the fairgrounds and then we start playing the screen when we go into the background and to do that. The first step is going to be to a
dependency on the XR play a coral Ivory and this provides the main interface the player that you can use to control playback for me than code. As you can see marks out of a dependency on the core library with the Russian 2.8.0, which is the new release that we've done this week. He's also a dependency this provide some customizable high-level UI components which you can put into applications for things like showing a video player interview last play of you which were going to use today provides playback control of you and some other components
customizable so you can change which icon do it using you can customize the layout and so on. So you can see that Mark has added a plan for you, which is going to fill the activity and he's giving it and identify a play underscore. If you said we'll be able to access it from card if we switch. We conceived in a long create message. We can get access to that View using find view by ID. I want to call access to that to you. The next step for going to take is to watch the initialize the plan. Okay. Once you go, I'll play with you in a field. Let's override the unstop methods
non-stop Meghan to initialize the plan. British light the fire at Waco in to use a static method on the exoplayer 5 3 code new simple instance. This is going to give us a simple instant pot. Chili simple exercises are recommended way of integrating at the moment because it provides it has lots of things for you for example handling the life cycle of the surface hold a video outfit repulsing and as well as a contact stattrak select implementation in this example, we going to use a different track Celexa, but this is an example of a person
which is very common and exercise API what you post in a dependency of the complainants. In this case. We're passing in a different truck tonight so that you could post in your own tracks like to implementation if you wanted to have much more control over how track selections working. Turn off somebody going to head and he's found the Plaid to the play of you so that the output is going to appear in that particular to you. Okay. So the next step is to tell accept I want we want to play I'm so that there are really two parts. The first one is to provide a Factory 4 dating sources
example using a particular HTTP suck or letting falls from the local device, which is used for HTTP URLs falls from the local desk assets and resources bundled into IDK and someone but if you wanted to you could use a Keurig Okay, that's the first part of the next part is specify a media Source. Your choice of media source is going to depend on the type of media. You're trying to play and this example, we going to be playing mp479 to use extract to Media source, which supports formats like MP4 MP3 matroska and 7
if you wanted to play Josh or achelous or smooth streaming streams, then you would use the corresponding media sources for those different formats. Are you can see that mocks pulsing in the URI? Tell MP4 when he's creating the video source. Okay, once you call a media Source now, we just need to prepare the player to stop buffering beta so we got to the prepared message. Family Guy into corset play when ready to tell the player that as soon as it transitions from the buffering states to the ready sight and Playback should begin with my sexy. I mean, I'm not full that remains is to clean up
when the activity goes into the background. So we'll ever ride on Destroy. That's why on stop. And in that we're going to clear the play reference on the play of you. Anime crying to release the plan. Okay. I know this is kind of the the minimum coverage. You need just took to play a video stream. Let's go ahead and got to the device now and if we could switch to the phone, please. Hopefully you haven't missed out anything. So when the application is being installed on the vice the device you should see the video slots to play with magic key
and you can see that if you tell me on the video of Isis Temple playback controls pretty much what you'd expect from a video player app. So you can pause and resume you can seek around on someone. Okay, that's not a switch back to you. Lone Star video app if you switch to the laptop, please and now we'd like to think about how we going to monetize our content by sharing ads. This is extremely easy to do with acts like that. We've recently added an IMA extension which makes it very easy
extensions around external functionality that make it very easy to use with actually flat in this case. The IMA extension is a wraparound the interactive media at STP which provide support for loading XML a text in Boston be metformin, which is a standard format for information about ads So we're going to be using that to Living Arts and then we can answer set with it within the contents in a fire. Play God's going to create a absoluta which is a we going to use an RMA out later, which is the provided by the IMA extension. This is going to take the
context which is the Euro of an XML document specifying what adds to play. We're in the content. So we're going to create the ABS light on create an event to release it in on destroy. The reason we're using on create an unjust Roy is the thought I'd slay that contains information about which I took already being played. So he wants to make sure the S5 goes into the background and comes back into the pool with multiple times. Okay, I'm as I mentioned before media sources of how we tell ex wife. I want to play and you notice play ads for use. The predictably named as media source, so we
can pray and ask media Source call Cinda content media source that we want to play football Saturday, which is going to be used to load. They took the plane the odds with us in the house later. And finally we're passing a few groups that's on top of the player and that's going to be used to show you any user interface associated with odds, like for example, the skip button. building a Nas media Source out of a Content media Source like this is an example of media Source composition and this is something Okay, so do we
prepare the plan without tomato sauce, and then we should be good together. So you can see but hopefully it happened is for what? We're able to add support for playing outs with baby channels play lines of credit and actually passed this way of integrating the SDK gives a really good user experience because that's our plan is about which I coming up in the future. I'm not means that it's going to be able to buy the content in advance and advance to give you seamless transitions between odds and content. So as you told me how to pre roll out of the beginning
and now the time boss has little yellow Marcus was just telling us that positions about Smith in the contents and exercise taken care of all of it the difficulty loading the odds and buffering everything behind the scenes for us. Stop playback an hour to talk about about audio. Thank you Andrew so far we focus on video on this application and look a little bit into audio playback more challenging because we want to enable to use it to put out replication into the background and use another application and audio should
still keep playing in the background for this. We are going to use a so-called foreground service. It's called it for round service because we need to attach at notification to the service and these notifications is in the foreground is visible to the USSR to use all the way to where we are doing some work in the background. Pizzeria such a note as such a foreign service is a mandatory requirement. If you don't put our service into the foreground the system will kill our service and the application pressure at starting point of our Foreign Service and flex switch
back to the laptop place yesterday. See here. We have this audio player service which extends from the service. You have already over right in their own create message Mary put the code from our video song book very initialise and prepare the player with a single me the items. They don't destroy their least our resources and finally announced. Commands with return stuff starts tickets. So the service is not immediately destroyed and we can explicitly terminate our service when we are ready. I finished with audio playback. That service also already registered in the
Manifest. We have a simple service elements which just points to our audio player Service close. And finally we need to have a way to start our service video discs in the main activity in the on create message. Main activity heavy Drake and intense pointing to our audio player service and then be used as a static method of the exoplayer library, which starts the service in foreground according to the to the API level this again, and let's change to code a little bit of very
initialize our player because we don't want to only support playing a single me the items. We want to support playlist so that you can move to the next door to the previous item. Can't just let the playlist play one item after the other. The sax player we are using a concatenating media source to implement playlist and play cat Presley and refile three buffering in between. So we have Chief a pretty nice user experience for flat for audio playback. Allows us to dynamically change the playlist we can add and remove media sources by the player is playing or we can even
move the currently playing item in our playlist Wild players playing at Spring needs Our Song. Airy and for each sample, we get a URI you again or going to create an extraction mediasource attend vs is expected major source to our concatenating major source in the end. We prepare our play orestes concatenating May 14th playlist, Chris Kappes play Capital speech play Capital Street and without repo repo frame. Announce, let's have a look at our service again and make it appropriate for ground
service. We've already said we need to attach a notification to our for round service. And for this we are going to use Steph player notification service manager the player notification Manager application for nicely in sync with the state if the player so each time the play estate changed the manager Phil update an application at post it to our driver again be used as static methods to create display notification manager and we need to post a couple of arguments with first. You need to pass the context then we need to pass the channels ID
which identifies our channel to which we post our notification the next part of a meter is a strain identifier which which gives us at localized name for our Channel. This is the name that shows up into settings dialog where are using may be 1/2 hour. Haitian Channel, and then next parameter is a notification ID to identify identify out notifications. And finally we need to pass a so-called mediator stretched. The manager will use our adapter to get information about the currently playing items. So each time a notification is revealed that. Please
call to get this information. Receipt for each of those callbacks two-player instances Paul's to the message so we can use this player hits player to get the current window index 2conv Indo index kid stuff to index to pick out a sample out of our samples Airy, which is kind of the playlist return the title to get account entitled and we have a pretty send a message with very just returned restrictions hbg longer text, which is displayed in the context. He needs a notification and finally, we also provide a big mess to make our notification a little bit
nicer a convenience mess up if I was down post to get such a big map for each of our samples. Finally, we also need to provide a pending intense this painting incentives fired from the use of cats on the notification and we want to get the use of back to her applications in history in our example to the main activity to activity. So betrayed and intense pointing to our main activity and then we wrap this intense into attending incense, which we are using with the static method get activities on defending itself against a couple of arguments to Counting
Stars the first argument than a request code that you see right now in case we are not going to use it in our sample send a 10 torrenting flowermate activity. I finally a flag to tell the system to update any pending intense already in 2 Q So now we have two player notification miniature being out and also need to register a notification listener listener makes our service ever of the life cycle of our notification. It has two methods. So we register at these notifications at night and
started is called when death notification is initially created here. We are now past those two arguments to that start for ground service at this is now the moment where our service is officially at for ground service till now safe. The system is not going to kill our service animal. The next mass at Saint is called my new notification is Council to give it just stopped sells our service because we want to tell me that the service. Finally, he also needs to register two player League set the play reflect set
player method on the notification manager of the notification that you can sync to play a state to the notification. So that's about it. We also need to clean up and down destroy Master TV just now to set the player instant again, so we don't leak at player a player reference Bandit service is being destroyed. So now please change back to the phone and if we done everything right we should now be able to hear the playlist playing in our service. The application comes up you see the notification in the drawer.
Playing. Let's have a look at the notification in the drawer. We see here the information which is comes from a rat after we can use the playback controls to control playback decompose Aunt start again recent skip to the next item and if we tapped on the application fee gets us back to that application. So fine, so at the next steps when we have seen now that this notification contains information about the current item and about the state and it lets the user control the media playback. What if we want to expose the functionality
to external application? Let's go to the neck and go back to the slides, please. cute Google Assistant, for example may want to allow the user to use voice commands to control media playback Android auto or wear OS not to use a remote control remote control and media application and brows in a media Catholic without application that has a solution for this because we essentially need to provide free things to let external applications participate in media
playback first. We need to expose the playback state is it stinks like the player state sells the current media item off the playlist refrig with prepared our player second. We need to be able to retrieve play back commands from external come on external application so we can execute those commands on our player instance and sir. We need to provide ways to browse in our major catalog. The Android framework provides a solution for this it provides the media session at the media browser service. We are not going into the detail of the media process service because we don't have time
for this. But will you look at me yesterday which allows us to exposed to play the XX and retrieve playback for moms? And I work out at some point. We are going to you set media session connector, which is part of the media session extension of the exoplayer library this me to session connected State versus state of the media section 303 scope mounts from the major session. So let's change again back to that laptop, please so we can see how we can do this in Korean. We are again in the old rate method of our player
of our audio player service and we are now going to create an instance of a media session session compound. We assign it to a field call me to session Copart Constructor with the context and and identify which is the which is just a string set hour meter session at to buy call back passing through to the vet active method. Now at the first step, we want to make the player notification mean that you're aware of our session and we passed the tokens to the play a notification manectric so we can in the end homes that need a side notifications and
provide an art and artwork for our lock screen of our device, but now the media section to the connector so he has access to the session. This that we want to synchronize our playlist with the queue of the media section. So external application know what I think we are having an hour playlist for this we are going to use a timeline to navigator. The timeline is the internal representation of the playlist of the player has been prepared. So as many windows in our timeline as we have items in our playlist. So,
where's this? We can't use the window index which is the parameter. We just passed through our mess up your going to override to create to get the samples again out of her and Ethan are going to create a media description for such a sample. Let's have a look in this Academy description method to see that there is not much magic behind it received. We are using at me just description comp at Fielder and then we populate the state of this potato from our samples and then refuse such as meter description compared which we return to our connector.
So now we've already this me to session connect of The Next Step again. We need to pass the player to the connections so he can sink the player fifteen media session if they can set you stats player methods. The first parameter is the play resolve the second for me to visit play Backstreet's pair. We are not going to use it because we don't want to allow external application to initiate play in our sample. So that's kind of it again and only destroy me need to clean up first. We just released the meter session and a second step the again set the player instant soon all this time
in the connect to avoid bleaching Fannin since of the player. So now that's kind of fits. If everything is correct. We should be able to deploy the application again, please switch to that phone again and Andrew should now be able to use Android Google Assistant to skip to the next item. Let's see if this works. Play next track. What fine well. I'm out. Now over to enter again for downloading in offline functionality. I think it's sweet spot to the slides, please.
Another feature of meteorites that I'm sure you're familiar with its support for downloading media for offline playback. This is really important. If your users are going to be in a situation where they have no connectivity you intermittent connectivity. And this is being a much-requested feature for a while now, and we really pleased that we've been able to launch this in all 2.8.0 are released this week. Nxr play I'll support for downloading is built on top of our support for cashing. So I'm going to say a little bit fits into the picture of the playlist downloading bills on Spotify.
This diagram is intended to show you the dataflow when playing One MP3 file. We have a song which is in the cloud somewhere. We have at a source, which is requesting not over HTTP. It's loading the enemy had a media source that was getting the information from that. I'm providing it to the playoffs. Let's see. How casting fits into this. Well, it's quite simple. We just out of cached data source that sits in between the plates or using for lighting from the network on the media sources providing ties to the player and it's going to be responsible for dealing with a cash assistance.
In this case for using a simple cash, which is provided by a school library. The way this works is as follows when the media Source request some data, it's been an All-Star as the cash. They supposed to provide that information. The couch light source is going to look in the cash. If it's present then that's great. We can hepatitis from local storage. Like I said the media so sad we never had to go to the network on the other hand. If the title is not present in this sauce is being used to light from the network. I just going to request that ice from that and then it's going
to store it in the cash when it arrives and in this way the cash budget egot populated with the date of this loaded while during playback. You can see also on this diagram we got a cash is Alexa. This component is responsible for making sure that our cash doesn't grow indefinitely as we play more and more different media items. Okay, now that we want to think about downloading so downloading is a set for operation. We she might want to inform in the background. So it might need a service and this downloading operation what's going on internally? Is it starting to read through the entire
stream? And if it does that it'll be populating the same instance that you're using for playback. And as you can probably guess when you do this operation, if you've populated then when it comes to playback your player is going to be able to get everything from the cash from the cash light source. There's never a needs to go to the network in this case. One very important thing to note is that we've got I know what kind of Effexor so this is an implementation of passive activities does nothing it's never going to be evicted from the cash. The reason we have is that we don't want to
have a situation whereby with downloading the stream and then when we download it up to a certain amount of Asa we start getting rid of a sore at the beginning of the stream cuz I will be very annoying for uses. Okay, so that's the theory. Let's see how this works in practice if we could switch back to the laptop, please. Communities in 3 steps the first step is we going to add support for cashing to all play on. Temp agency employment and audio download service which is going to actually run the time like operation as in the background. And finally, we going to add very
minimal pizza. If you are a two alright tivity and noticed a lot of trick of the downloads to take place. Okay, so we'll go ahead and create a cash latest news 530 on the slide. This is going to sit between the ages was Factory for lady from the network on the media source, which is actually going to be extracted media. So you can see where using a utility method to get all cash. Let's jump into that and see what's going on that. As I mentioned we're sharing the same cash for playback on downloading and that means it's important that we just have a Singleton for a whole process.
Inside get cash. What we're doing is we got one already. If we don't have one we going to create a file which is pointing to the right tree weather going to throw the downloads again, not directory. I know so I know what price is right side, which is very important because we don't want to be removing dates from the cash. So that's how we make a simple cash. We also need to provide the cast. I suppose fat tree with the Upstream data source Factory for loading data from the network. Okay, I'm out. So we need to Renaissance music on state. So smart tree. Let's introduce a local
variable for that and then we're going to use that kind of factory in place of Rights was fight for you. We were using before from the network. That makes sense for adding support for casting to all play on now. We're going to move on and we going to add an audio download service, which is actually going to run the downloading operation. We provide a super class for a service for your services will be inheriting from that hair. Okay. It is a few things
tstats we need to do now the most important one important not to forget is to add a declaration for the service to your manifest. Now we can switch back and we have to implement a construct two and three methods that start doing that will create a construct 2 with my events, but then we got to cool to see if you can start some information about the download notifications. So we'll need a notification ID which must be different from the next vacation ID. We used to audio playback an interval in which to update the next occasion on which is the default have with
pasta and a channel ID for a download notification Channel. Finally, we also need to provide a string results which describes the United Nation Channel playback. Now we need to override some methods. Say that's having a nice. The first one is we need to provide a download manager and we haven't utility method for this which will have it looked at in a second. That's just taking your contacts. Let's jump in there using a Single Fin instance of a Download Manager for a whole process
of creating a system information about us dating app download manager pulsing in the same cash assistance as the one that was using for the cash during playback. You can see all the cash realize it from Progressive download actions because we downloading Progressive media. Okay. Look at the schedule for the purposes of Austin. We just going to leave this returning. No, but in a real application, you would want to provide some kind of job scheduler so that the system can start your download service running a notice of resume downloads. You can have a look at also has an app for an example of how
to be. Finally. We're going to provide a progressive education and we've got a handy utility message to build one hit. This is going to take a contact an icon which is going to be starting in the next vacation associated with the download that's in progress. I want to see is I'll play back home so that we provide the download Channel ID, which is the same one that we used earlier. I just want that Silly String all the results. 1.1 promises for a headset for
an intense next which will just cause 4 Can we go to bed mixed up with that promises here? So if you remove download channel name. Okay, send a message has no content intent is no the message is no I will just pass our task states which has information about the downloads in progress what their status is? That's a lot of promises. Valley Sporting Goods download service the final stuff I promised is just providing a way to trigger these downloads for my activity. So as you can see, we've got an empty list you here we can replace spot where the list of samples that we
just got three items in a playlist. When the Music Stops one of those big trying to trigger the download to start and to do that, we will be creating a progressive download action if you were downloading a different type of content like a dust dream and you would not use Progressive download action you do with the corresponding download action for the typist Junior downloading That's going to take are you? All right. It's going to take a flag to say whether we're leaving or not. We want to add it to the cash. They will pass in fultz. If you don't want to say any cuss and a toaster will pass
know for the data and the custom cache key. Once you created a progressive download action All That Remains is to cool at method on the download service. There's a static message to start the service without action. Takes a context, which is main activity. This item click listen to this. Okay, it takes the closet to download service, which is audio download Service. Plus. It takes the town that action we should just create it Progressive download action. I finally we can Post Falls to foreground because our activities already in the
foreground. Okay, that's quite a few bits to run but I've got everything. Let's give it a try and see if it works. Now we have plans to show you putting the device into flight mode and playing by the downloaded content found fortunately. We're having to use cost to play the street and so we might actually be able to show you the keys to imagination as you can see when we talked the items. You got a little play icon which. Download notification. I'm not spraying to progressed as the downlight progresses place in the cash in the background.
So that's pretty much it for downloading. I'm going to head back over to Mark's to say a few words about cost to take out cover in code and revision recovery code is that integration with Google called Google, It's almost expected by all users verify users to be supported by at media apps recently added at Calais extension which makes integration with cost for extra player application for the easier. Let's switch to the lights again, please. Do you take off extension extension takes long to each of the player interface of the exoplayer library all that you are components and other
components like to play a notification miniature need a special connection between seen in action take advantage of this player in the face and only rely on this player in the face the most prominent implementation of this player interface. It's a simple excerpt almost all ex-player applications used to Simplex 2 player for local play back the cost extension now adds another implementation of his player interface fish is the cost player the cosplayer wraps remotes meet a client of the cost at the K. So it gets pretty easy to swap local play back down with the Simplex 2 player
with remote playback of the cost because all those components of the XO player Larry and hopefully also your application only rely on the player interface you can exchange is low. Play back with remote playback. It quite easily get up repository and also at block post on Media or medium, which shows you how to set up the car parts at for your application. Okay. Thank you Munch. The Remains ready is to provide you with a link to the brunch, which has the color that represents night, or at least it'd be very similar to the codes
represent today. And please go and check her out and Trice out that you actually be able to try that you download functionality if I put in your device into flight mode and making sure that it plays it back now, so please check that out. We have an office hours session today at 3:30. Please come along if you could questions and lots of members of the team will be that and we'd be delighted to meet you and find out about what you're doing and how we can help you with that. This is a link to our get help project. If you if you haven't seen that before we have a developer guide, which
takes you through the prices of getting started with Excel player at a slightly slower pace and it helps to explain some of the backgrounds, and I'm finally as Mall Collegiate to we also have a Blog on medium when members of acts like fire team are writing blog post about new functionality that we working on. So, please do check that out and subscribe and thank you very much.
Buy this talk
Access to all the recordings of the event
Buy this video
With ConferenceCast.tv, you get access to our library of the world's best conference talks.