简体   繁体   中英

Play mp3 file given a url directly to the mp3

I want to be able to play an mp3 file in my android application given a url (eg http://www.somedomain.com/audio.mp3 ).

This I think should be used to get a byte array using an InputStream maybe but I'm not totally sure, which then can be used with a MediaPlayer object.

Read through the SDK pages. Playing from a File or Stream

I found in practical life that the MediaPlayer can play just about any MP3 URL if you take care to reformat any Space characters (0x20) into "%20". I tried that URLEncoder and some other filters to little gain but simply converting those spaces before presenting the URL to MediaPlayer will take you far.

In a String you can easily substitute stuff:

mediaPlayer.setDataSource(urlString.replace(" ", "%20"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM