Downloading an audio file from a URL is a very common process and most of the app have this functionality. It is really simple to download an audio from a URL and for that, you need to create an object of DownloadManager and enqueue the DownloadManager.Request object into it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Uri file_uri = Uri.parse("your_file_url"); DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE); DownloadManager.Request request = new DownloadManager.Request(file_uri); //Setting title of request request.setTitle("Audio Download"); //Setting description of request request.setDescription("Android Audio download using DownloadManager."); //Set the local destination for the downloaded file to a path //within the application's external files directory request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/", "your_audio_name.mp3"); //Enqueue download downloadManager.enqueue(request); |
As you can see in the above code segment a download service is taken and an object of DownloadManager.Request is created with the file URI. You can use the setTitle and setDescription function to the title and description respectively which will be displayed when the notification bar is expanded.
function setDestinationInExternalPublicDir is used to set the destination of the file in the external memory. Environment.DIRECTORY_DOWNLOADS defines that the file will be stored in the downloads directory.
After creating the request all you need to do is use the enqueue function of the DownloadManager to start the download.
Thank you very much, this is Vedesh Kumar signing off.