How to read remote mp3 file?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jyri_Saastamoinen
Posted On:   Wednesday, May 12, 2004 07:31 AM

When I try to read it like this: URL u = new URL(url); URLConnection ucon = u.openConnection(); BufferedReader in = new BufferedReader( new InputStreamReader( ucon.getInputStream())); FileWriter mp3Writer = new FileWriter("first.mp3"); String remotefile = ""; while ((remotefile = in.readLine()) != null) { mp3Writer.write(remotefile); } mp3Writer.flush(); mp3Writer.close(); it can't be played. What is the right way to read that file? Tia JJ    More>>

When I try to read it like this:

			

URL u = new URL(url);

URLConnection ucon = u.openConnection();

BufferedReader in = new BufferedReader(
new InputStreamReader(
ucon.getInputStream()));



FileWriter mp3Writer = new FileWriter("first.mp3");
String remotefile = "";

while ((remotefile = in.readLine()) != null)
{

mp3Writer.write(remotefile);

}

mp3Writer.flush();
mp3Writer.close();



it can't be played. What is the right way to read that file?

Tia JJ    <<Less

Re: How to read remote mp3 file?

Posted By:   David_Thornton  
Posted On:   Wednesday, May 12, 2004 06:43 PM

An MP3 file is a byte stream not a character stream so you should not be reading it using BufferedReader.readLine() method. I doubt there are any new line characters in an MP3 file that are actually meant to be.


You are also using the BufferedReader as a wrapper for an InputStreamReader. An InputStreamReader reads bytes and converts them to characters using the default or specified character encoding.


What you need to do is just read and write raw bytes.


Your code should probably be along these lines;



try {
URL u = new URL(url);
URLConnection ucon = u.openConnection();
FileWriter mp3Writer = new FileWriter("first.mp3");
InputStream in = ucon.getInputStream();
int b = 0;
while ((b = in.read())!= -1) {
mp3Writer.write(b);
}
mp3Writer.flush();
mp3Writer.close();
}
catch (Exception e){
System.out.println(e);
}
About | Sitemap | Contact