How to download and play a song (.mp3, .m4a) if we know the url in android

I need to download a .mp3 or .m4a file from the server using the url and then play this song.

Any ideas?

+4
source share
2 answers

Try this code in your file:

USING

url = "your url name+filename.jpg,mp3,etc..." FileName = "/sdcard/savefilename" // save in your sdcard try{ java.io.BufferedInputStream in = new java.io.BufferedInputStream(new java.net.URL(url).openStream()); java.io.FileOutputStream fos = new java.io.FileOutputStream(FileName); java.io.BufferedOutputStream bout = new BufferedOutputStream(fos,1024); byte[] data = new byte[1024]; int x=0; while((x=in.read(data,0,1024))>=0){ bout.write(data,0,x); } fos.flush(); bout.flush(); fos.close(); bout.close(); in.close(); } catch (Exception ex) { } 

and after you want to use MediaPlayer and create a media player object in your activity and play.

 mp.reset(); mp.start(); 

like this.

I hope this helps you a lot.

+5
source

There are many questions about how to upload a file in android. Just do a search.

Alternatively, you can use android MediaPlayer to play a file from the Internet without downloading it.

-4
source

Source: https://habr.com/ru/post/1332086/


All Articles