Reputation: 1298
I need to download a .mp3 or .m4a file from the server by using an URL, and then play that song.
Any ideas?
Upvotes: 4
Views: 3435
Reputation: 369
Try this code in your file:
USE
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 object of mediaplayer in your activity and play.
mp.reset();
mp.start();
like this.
Hope this will help you a lot.
Upvotes: 5
Reputation: 93183
There are plenty of questions of how to download a file in android. Just search.
On the other hand, you can use android's MediaPlayer to play the file from the internet without downloading it.
Upvotes: -4