In this video, get a coding walkthrough on how to create a MediaPlayer service app. Learn about the library needed, creating the service class, and creating and setting up the MediaPlayer instance.
- [Instructor] In this section we will go through the process of creating a MediaPlayer service. This will cover how to add the needed libraries and create a service. Let's start out by starting Android Studio and opening up the MediaPlayer service starter project. To implement our service, we will take advantage of Google's media library. To do this, you would add the media compat library to the app gradle file. Here we've added the support media compat library, version 28.0.0. If you've opened up the starter project, this will already have been added for you. We've also already added the service and provided some methods to get you started. Let's open up the MediaPlayer service. Here we see that we've subclassed the media browser service compat class. This class handles some of the media connection duties. The onload children and get root connection methods are for services that provide lists of media to devices like connected autos. Look through the different methods as there is a lot here. Let's go to the createMediaPlayer method. Let's go ahead and close some of these windows out so we have some more room. Let's start entering some code. First we'll check to see if our mediaPlayer is null. If it is, we need to create one. And we'll use the apply so that we can do more things inside here. We're going to set the audio attributes. We'll use the AudioAttributes.Builder class to do that, and we'll add a few other things here. Let's set the content type, and we'll use the audio attributes music type here. Now that we've got that built, let's put a try statement, try catch statement in here, because we're going to want to catch all the exceptions that could happen here, and then we'll log this. And we'll say there's problems loading audio, and we'll print out the exception. Inside our try block we'll add some code. We're going to set our WakeMode first, so that we can tell it to stay awake. We'll use the power manager's PARTIAL_WAKE_LOCK that'll keep it open for a while. And we first need to set a context here. And then we want to set the state that we're going to be in, and that will be preparing. Next we'll want to set the DataSource, and this is what we're going to be loading to play, and we'll use our PODCAST_URL. Next, we'll set our set on preparing listener which will be this media player, because we were implementing that interface. And then we'll call prepareAsync, which will load the file in the background. So this will create the media player, set the stream type, set the WakeMode, and set the DataSource. We set our service as a PreparedListener, and then start to prepare the media.
- Why your app needs audio and video
- Controlling volume with AudioManager
- Playing audio with MediaPlayer
- Playing multiple sound files at once with SoundPool
- Creating a VideoView UI
- Playing media in the background
- Audio and video formats
- Working with ExoPlayer