Playing Audio file online and offline

Updated 22 April 2017

Save

Playing audios directly without downloading it is an easy and fast way to use media and it also saves device memory. Playing media online also saves you from the headache of management of files in the device.

To stream audios, android provides a class MediaPlayer which is used to play online or offline media files in the app.

You can follow the below code segment and the explanation after that to stream the media directly from the URL.

As you can see in the above code segment that an object of MediaPlayer is created and the url is passed to the function setDataSource().

start() function is used to play the audio file on the URL.

Similarly to play the downloaded audio file. You can follow the below given code segment and the explanation following it.

This is similar to the playing media online except that you need to create() function and pass the file path to the as the parameter.

To perform some task after the completion of audio. you can use the MediaPlayer.OnCompletionListener() function. Check the code segment below

You can also use the isPlaying function to make a single button for play and pause.

 

Thank you very much, this is Vedesh Kumar signing off.

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home