This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes reading an MP3 from a URL to a local file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "reading an MP3 from a URL to a local file" Watch "reading an MP3 from a URL to a local file" New topic
Author

reading an MP3 from a URL to a local file

Petereyup Smith
Greenhorn

Joined: Aug 18, 2011
Posts: 7
I am trying to copy an MP3 from my website to a local file - the following program SOMETIMES!!?? works, sometimes it get so far and then stops -it does not end just sticks at a random point,and i have to cancel the program ( I am using Netbeans), but it does work on the odd occasion?! my main aim to have this in a swing application using swingworker, and to update a status bar etc.....

Any help would be greatly appreciated!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

Welcome to the Ranch!

That > 0 should be >= 0, > -1 or != -1. 0 itself is a perfectly valid return value that does not indicate the end of the stream. That's what -1 is for.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Petereyup Smith
Greenhorn

Joined: Aug 18, 2011
Posts: 7
I have tried !=-1 and >=0, it still sometimes works and sometimes just get stuck within the loop, but no new println's are produced - it just hangs!

Thanks for the tip though!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

If you've modified the loop as I suggested, then I don't see anything wrong with your code. Perhaps there is a problem with the network, or with the server you're getting the MP3 file from. Can you download the file from a regular browser?
Petereyup Smith
Greenhorn

Joined: Aug 18, 2011
Posts: 7
I was wondering if it was the network - sometimes when I watch a streamed video through BBC's Iplayer, it stops ,then starts again with the sound out of sink with the video, and internet radio sometimes just stops!! - I will try it on a friends network.

Thanks
Petereyup Smith
Greenhorn

Joined: Aug 18, 2011
Posts: 7
I have done some more research : downloading small(ish) files works fine, but larger files can just freeze the program.

What I have found out, is the connection could be timing out after a certain time, I then thought could it be done using FTP - I use FileZilla and that works! - Nope does the same.

Can anyone please give any suggestions - could something to do with sockets work ( I am what you might call a beginner to java!)

Thanks
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19656
    
  18

If FileZilla has the same problem, the problem is somewhere in the network. Since the problem is not limited to this one server, that problem can be your network card drivers, network card itself, cable, or anything beyond that. Start locally, by reinstalling / updating the network card drivers, then work your way along the line.
Petereyup Smith
Greenhorn

Joined: Aug 18, 2011
Posts: 7
Sorry - FileZilla DOES work - that's what's weird!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: reading an MP3 from a URL to a local file
 
Similar Threads
copying contents of one file to another
File upload
Command line arguments
using byte array to copy contents of one file to another
Downloading File From URL