Play mp3 file while downloading?

2019-03-27 12:17发布

问题:

I'd like my Android application to download an mp3 file from the internet and play it like a stream while downloading it. Is this even possible? How would I go about doing it?

Essentially I want the user to be able to listen to the file instantly, but have it keep downloading to the SD Card even if he stops listening, so the whole mp3 file will end up on the SD Card either way.

回答1:

I don't believe android provides the functionality you're asking for. But there's one workaround I know of that might work.

http://code.google.com/p/android/issues/detail?id=739 is an open ticket with a lot of discussion that relates to what you're describing. In the 5th comment seeingwithsound says

However, as a workaround I next stream my synthesized data to temporary files much like you consider streaming your HTTP streams to files, for subsequent playback of these files via MediaPlayer. [...] It looks though like your problem might be served by more control over MediaPlayer's buffering, because you are essentially concatenating streams through some cache mechanism (buffer) ...

To be honest I haven't played with MediaPlayer too much, so I can't give you too many details on how to "control MediaPlayer's buffering". But assuming you can get that to work, I suggest that you download your .mp3 file to (a) temporary file(s) which you can then point MediaPlayer to.

Sorry this answer is so vague, but you're entering territory that few others have before attempted. Good luck.



回答2:

For Android devices 2.3 and earlier, you need to remove the Content-Disposition response header altogether. This will cause the browser to invoke the audio player of the user's preference.

Try it here: http://declinefm.com/archives select an archive of your choice, and tap the download link.

For modern Android devices, you can dispense with the removal of the CD header.

I have a tutorial here that I wrote inspired on your post:

http://rudd-o.com/linux-and-free-software/how-to-properly-stream-audio-from-your-plone-varnish-site

Enjoy!



回答3:

RTSP or HTTP? If HTTP you can try and remove the Content-Length header which on some mobile devices will allow for immediate invocation of the player. It sounds as though you're pumping the data through a local app. How are you playing the file now?