0

I am trying to play a song from java and I have successfully played a different song from the same code (but to a different URL). I found out that most of the sites that you can upload files to (dropbox etc.) require you to be signed in with cookies and such. I know java doesn't have my gmail login and access to my dropbox account. I am looking for either a way to get the file locally (I did and it can tell me how big the file is but not play it(GRRRR)) or a website that keeps the files uploaded without any protection. Here is the code:

package playsound;
import java.net.URL;
import javax.sound.sampled.*;
import java.io.File;

public class LoopSound {

  public static void main(String[] args) throws Exception {
      System.out.println("Working Directory = " +
              System.getProperty("user.dir"));
      File file = new File("c:/Users/liam/workspace/playsound/song.wav");
      System.out.println("length : " + file.length());

  URL url = new URL(
          "https://www.dropbox.com/s/n9plhbvwy7drhvy/song.wav?dl=0");
          //"http://www.intriguing.com/mp/_sounds/hg/fart.wav");   

Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.
  getAudioInputStream( url );
clip.open(ais);
clip.loop(5);
javax.swing.JOptionPane.showMessageDialog(null, "Close to exit!");
  }
}

The Fart thing was the one that did work for me. I will clarify if needed.

Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at playsound.LoopSound.main(LoopSound.java:20)
informative
  • 53
  • 2
  • 13
  • AFAIK URL and URLConnection can handle cookies, but I prefers to use the Apache HTTPClient API personally... – MadProgrammer Jan 18 '15 at 20:28
  • " I am sure that java doesn't have the cookies to do so" - This sentence makes no sense. What exactly do you mean? – Maroun Jan 18 '15 at 20:28
  • I meant it doesn't have access to my dropbox account because i still need to sign in. I would go to the link and all it would do is try to play a song from a page thats says" You can't see this song, login to do so." – informative Jan 18 '15 at 20:33
  • Have you tried downloading the file via a `URLConnection` and attempted to simply play the file from disk? It's entirely possible that the `AudioSystem` is incapable of determining the audio file format, because the URL does not end in `.wav`, but in `dl=0`. So saving with `.wav` and then loading from disk, may work... Just an idea. – Hendrik Feb 17 '15 at 20:45
  • Or perhaps what you are downloading via that URL us not a sound file at all, but some login screen... who knows, unless you actually look at the content of the downloaded file. – Hendrik Feb 18 '15 at 07:46

0 Answers0