Streaming an audio media consists to receive constantly data from a remote source and to deliver the audio data received to the end-user. Nowadays, everyone uses streaming platforms daily. For example, YouTube is a streaming video platform.
In this tutorial, you are going to discover how to implement audio streaming in an Android Application. Note that you can also discover this tutorial in video on YouTube :
To implement audio streaming in our Android Application, we are going to use the MediaPlayer API of the Android SDK. Like said in the Android official documentation, the MediaPlayer class can be used to control playback of audio/video files and streams. So, it’s perfect for our usage.
Create the User Interface
First, we are going to create a simple User Interface with a button which will be used to launch or pause the audio stream :
Write the Java Code
Now, we can write the Java code of our Main Activity. In the onCreate method, we are going to create the MediaPlayer object and to define the audio stream type to use. Here, we will use the AudioManager.STREAM_MUSIC constant. Then, we create a ProgressDialog which will be used to display a waiting message to the end-user when the audio stream will be buffering.
We install an OnClickListener on the button of our application. When a user will click on this button, we will check the state of the audio stream. If we are in pause, we will need to start the audio streaming. In this case, there is also two possibilities. First, we have not initialized the connection between our application and the remote audio source to stream. In this case, we will need to initialize the connection in a separate Thread thanks to a Player AsyncTask.
In the case where the audio streaming has just been paused, we will just have to restart if by calling the start method of the MediaPlayer instance created previously. If we are in play mode, we have to pause the MediaPlayer by calling its pause method.
Like said previoulsy, we need an AsyncTask to connect the MediaPlayer to the remote audio source. In the doInBackground method, executed in a separated Thread from the UI Thread, we start by setting the data source to stream on the MediaPlayer.
Then, we set an OnCompletionListener on the MediaPlayer. When the MediaPlayer will have terminated the audio to stream, we should call the stop method of the MediaPlayer and its reset method to let a future stream on this object.
Finally, we call the prepare method of the MediaPlayer which it lets us to connect our MediaPlayer to the remote audio source to stream.
Note that in the onPreExecute method of the Player AsyncTask, we show the ProgressDialog to the end-user and in the onPostExecute method, we hide the ProgressDialog and we will start the audio streaming by calling the start method of the MediaPlayer.
That gives us the following code for the Main Activity and the Player AsyncTask :
Last step is to try our Android Application and to enjoy the final result with an Audio Music File streamed :