You will start with playing a video file in the raw directory. If MediaPlayer is in this state you can call the start() and play audio or video again. Additionally, invokes onCompletion.onCompletion().
PlaybackCompleted State: When the playback is complete, MediaPlayer is in this state.If MediaPlayer is in this state and you want to play the media again, you have to prepare it again by calling prepare() or prepareAsync(). Stopped State: MediaPlayer is in this state when media stops playing.Paused State: When you pause the media, MediaPlayer is in this state.While the music or video plays, MediaPlayer is in a started state. Started State: Once MediaPlayer is ready, you can play the media by calling start().Once prepared, it reaches this state and calls onPreparedListener(). Prepared State: Before you play any media from a file or a URL, you need to prepare your MediaPlayer by calling either prepare() or prepareAsync().Be aware that you can only set a data source when MediaPlayer is in an idle state or it’ll throw an IllegalStateException. To set a one, use setDataSource() method. Initialized State: MediaPlayer reaches this state when you set a data source.However, you can catch the error using MediaPlayer’s onErrorListener.onError() callback. Error State: You reach this state if you try to play, pause or stop an uninstantiated MediaPlayer object.At this stage, you can’t play or pause the media. End State: Calling MediaPlayer‘s release() method frees resources and moves it to the end state.If you try to force it, the app might crash. You also reach this state after you call reset().Īt this stage, you can’t play, pause or stop the media. Idle State: MediaPlayer is in an idle state when you first instantiate it, or first create it using the new keyword.That’s a lot of information to take in, but before you can use the MediaPlayer API efficiently, you need to understand these states, so let’s take a look at each of them. When a media file plays, the MediaPlayer API goes through several states, which are summarized in the diagram below: It also streams music or video from a URL. MediaPlayer is a part of the Android multimedia framework that plays audio or video from the resource directory and gallery. You don’t want the user’s device to fall asleep and lock while they’re watching a video on your app, do you? Setting this flag ensures your screen stays on, while the app is in use. Notice this line inside onCreate() which adds a flag window.addFlags(_KEEP_SCREEN_ON). You’ll understand these implementations once you start working on the functionality.Īlso, to keep your app from falling asleep, you’ll need to keep your screen on. The class implements a few interface classes to manage MediaPlayer and seekBar callbacks. VideoActivity.kt: This might look a bit overwhelming, but if you skim through and read the comments, you’ll find it quite simple.An ImageButton to play and pause a video.Two TextViews and a SeekBar to show progress.A ProgressBar to shows the user it’s loading a video.activity_video.xml: This is the one and only layout file in the project.In this tutorial, you’ll play a video from a URL, so you’ll need the INTERNET permission. AndroidManifest.xml: At the top of the manifest file you’ll find.Navigate to these three files and check out their contents: Understanding the codeīefore the hands-on part of this tutorial, take some time to understand the codebase you’ll build on. You’ll implement the functionality throughout this tutorial. The app isn’t interactive yet because the starter project only consists of UI and some basic code. Extract and open the starter project in Android Studio.īuild and run.
HOW TO CHANGE DEFAULT VIDEO PLAYER ON ANDROID WHILE ONLINE DOWNLOAD
If you’re a beginner, check out our Android: Beginners tutorial first.ĭownload the materials using the Download Materials button at the top or the bottom of this page. Note: This tutorial assumes you’re familiar with Kotlin and Android development.