Learn about the different ways to play media and which widgets to use. The instructor talks about the built-in MediaPlayer widget and when to use the ExoPlayer library.
- [Instructor] You can play multimedia with either Android's built-in widgets, or you can use a third-party library, like Google's ExoPlayer. Android's widgets have the advantage of being built into the system, but have the problem that they work differently on different versions of the operating system. Google's ExoPlayer has the advantage of coming as a library that can be updated every time you update the app. You are assured that the version you are using is the same on all devices because you are including it in your app. It is also more extensible than Android's widgets. Unfortunately, it is also a lot more complex. Android classes are MediaPlayer for playing audio and video, VideoView for playing videos, and SoundPool for playing multiple smaller sounds at once. All of these classes are pretty easy to use, but have limited functionality. Use the MediaPlayer if you have really simple needs. It's also a good way to get started, but you may find that you outgrow it after a while. I found that ExoPlayer handles more media types and streams better than MediaPlayer. SoundPool is one of those classes that stands on its own. It's very useful for pre-loading sounds and playing multiple sounds at once. The video player class is useful for videos on the screen, but is limited. It doesn't handle screen sizes well with different video sizes. If you want to maintain the aspect ratio of the video and not have it look stretched or distorted, you'll have to do it yourself. If you know your videos will be only one size and will fit on all screens, then it could work for you. If you have more advanced streaming videos, ExoPlayer is a better bet. Android's AudioManager allows you to control the volume and audio focus, which we will talk about in more detail. Normally, when the user hits the volume up and down, it will change the ringtone volume. You can use AudioManager to make these buttons control the media volume, so that when they are listening to your audio video, they will change the volume of the media and not the ringtone. In order to actually play audio and video, you need to ask the audio manager to get Audio Focus. This tells the system that you want to play audio, and if it grants you permission, you can start playing. If you aren't given permission, then don't play. The reason this is necessary is that multiple apps can try to play audio at once. If two media players are trying to play audio at the same time, the user will get pretty upset. You can also find out when other audio's trying to play, like map directionw, and you can either stop playing or lower the volume while the other audio plays. What happens when your phone goes to sleep, and you want to continue to play audio? We'll explore WakeLocks to prevent the phone from going to sleep when you're playing. WakeLocks are a way to tell the system that you're doing something that requires the phone to stay awake. This way, your audio will not shut off. You have to be careful with WakeLocks, as it can have a negative impact on the battery. Always release a wake lock when you are done. We'll also cover the MediaSession class that helps in controlling media on other devices, like watches and other Bluetooth devices. This class handles the communication between devices, so that you don't have to write special code to show media controls on other devices, like your smartwatch or car. When you stop the audio on your watch or car, it will tell your app to stop.
- 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