Guest User

Untitled

a guest
Jan 24th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. import java.io.BufferedInputStream;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.net.HttpURLConnection;
  5. import java.net.URL;
  6.  
  7. import javax.sound.sampled.AudioFormat;
  8. import javax.sound.sampled.AudioInputStream;
  9. import javax.sound.sampled.AudioSystem;
  10. import javax.sound.sampled.DataLine;
  11. import javax.sound.sampled.SourceDataLine;
  12.  
  13. public class Starter {
  14.  
  15. public static void main(String[] args) {
  16. AudioInputStream din = null;
  17. try {
  18. URL url = new URL("http://www.roblox.com/asset/?id=27697741");
  19. HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
  20. InputStream bufferedIn = new BufferedInputStream(httpcon.getInputStream());
  21. //AudioInputStream in = AudioSystem.getAudioInputStream(Starter.class.getResourceAsStream("338876528.mp3"));
  22. AudioInputStream in = AudioSystem.getAudioInputStream(bufferedIn);
  23. AudioFormat baseFormat = in.getFormat();
  24. AudioFormat decodedFormat = new AudioFormat(
  25. AudioFormat.Encoding.PCM_SIGNED,
  26. baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
  27. baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
  28. false);
  29. din = AudioSystem.getAudioInputStream(decodedFormat, in);
  30. DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
  31. SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
  32. if(line != null) {
  33. line.open(decodedFormat);
  34. byte[] data = new byte[4096];
  35. // Start
  36. line.start();
  37.  
  38. int nBytesRead;
  39. while ((nBytesRead = din.read(data, 0, data.length)) != -1) {
  40. line.write(data, 0, nBytesRead);
  41. }
  42. // Stop
  43. line.drain();
  44. line.stop();
  45. line.close();
  46. din.close();
  47. }
  48.  
  49. }
  50. catch(Exception e) {
  51. e.printStackTrace();
  52. }
  53. finally {
  54. if(din != null) {
  55. try { din.close(); } catch(IOException e) { }
  56. }
  57. }
  58. }
  59.  
  60. }
Advertisement
Add Comment
Please, Sign In to add comment