Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Media Player: Download to File and Stream From File

Tags:

I'm using Android's MediaPlayer to play MP3 files that are simultaneously downloaded from the internet using the DownloadManager. I'm aware of the MediaPlayer's capability to stream directly, but I don't want to use that:

The obvious advantage you get by downloading and simultaneously streaming from file is that this way, the file gets stored and is available locally afterwards.

  • This works in principle - the MediaPlayer starts to play the file even if it not fully downloaded. (BTW my problem is NOT that I can't get MediaPlayer to play.)
  • The problem is that when the MediaPlayer gets to the position where the download status just was when it started playing, it stops and calls onCompletion. (Clarification: Let's assume the file is 12% downloaded when I start playing. When the player gets to the position at 12%, it stops.)
  • Quick side note: OnBufferingUpdateListener.onBufferingUpdate(...) is not called when something is appended to the MP3 file via the DownloadManager.

So the question is: How can I simultaneously download an audio file to the file system and play it? (Android 4+ suffices, if that makes a difference.)

like image 876
Hinton Avatar asked Dec 13 '12 16:12

Hinton


2 Answers

I assume you're pointing MediaPlayer to the file. In that case I don't think it supports streaming, and it probably checks the size of the file and such in advance, so it won't update itself as more of the file comes in.

You have to point it to a content source that will work with streaming, e.g. HTTP. I've just checked how "ES File Explorer" does it (Using LogCat) - when you click on a video file in a remote file share, it opens an local HTTP server, and sends to the MediaPlayer an HTTP Uri to its local server, that also includes the remote path.

When it gets the request through HTTP, the local server starts reading the file from the remote location, and providing it locally to MediaPlayer through HTTP. You can probably do the same, and in addition to providing it via HTTP also save it to disk.

like image 157
Oren Avatar answered Oct 06 '22 01:10

Oren


It helped me, I hope to help someone else.

Player download & play music.

mMediaPlayer.setDataSource(DOWNLOAD_URL);     File root = Environment.getExternalStorageDirectory();    File dir = new File(root + "/Folder");    if (!dir.exists()) {         dir.mkdirs();  DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);             Uri downloadUri = Uri.parse(DOWNLOAD_URL);             DownloadManager.Request request = new DownloadManager.Request(downloadUri);             request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_MOBILE | DownloadManager.Request.NETWORK_WIFI);             request.setAllowedOverRoaming(false);             request.setTitle(TITLE);             request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);             request.setDestinationInExternalPublicDir(Environment.DIRECTORY_MUSIC, TITLE);             request.allowScanningByMediaScanner(); 

After downloading file:

mMediaPlayer.setDataSource(Environment.getExternalStorageDirectory() + "/Folder/" + TITLE + ".mp3"); 
like image 37
Nikita G. Avatar answered Oct 06 '22 03:10

Nikita G.