2014-01-27 2 views
0

Я пытаюсь написать код, чтобы поток от SHOUTcast сервера и я использую приведенный ниже код и дайте мне javax.sound.sampled.UnsupportedAudioFileException , как я могу решить исключениеИсключение Shoutcast и Java

public static void streamSampledAudio(URL url) 
     throws IOException, UnsupportedAudioFileException, 
       LineUnavailableException 
    { 
     AudioInputStream ain = null; // We read audio data from here 
     SourceDataLine line = null; // And write it here. 

     try { 
      InputStream is = url.openStream(); 
      BufferedInputStream bis = new BufferedInputStream(is); 
      ain=AudioSystem.getAudioInputStream(bis); 

      AudioFormat format = ain.getFormat(); 
      DataLine.Info info=new DataLine.Info(SourceDataLine.class,format); 

      if (!AudioSystem.isLineSupported(info)) { 
       AudioFormat pcm = 
        new AudioFormat(format.getSampleRate(), 16, 
            format.getChannels(), true, false); 

       ain = AudioSystem.getAudioInputStream(pcm, ain); 

       format = ain.getFormat(); 
       info = new DataLine.Info(SourceDataLine.class, format); 
      } 

      line = (SourceDataLine) AudioSystem.getLine(info); 
      line.open(format); 

      int framesize = format.getFrameSize(); 
      byte[ ] buffer = new byte[4 * 1024 * framesize]; // the buffer 
      int numbytes = 0;        // how many bytes 

      boolean started = false; 

      for(;;) { // We'll exit the loop when we reach the end of stream 
       int bytesread=ain.read(buffer,numbytes,buffer.length-numbytes); 
       if (bytesread == -1) break; 
       numbytes += bytesread; 

       if (!started) { 
        line.start(); 
        started = true; 
       } 

       int bytestowrite = (numbytes/framesize)*framesize; 

       line.write(buffer, 0, bytestowrite); 
       int remaining = numbytes - bytestowrite; 
       if (remaining > 0) 
        System.arraycopy(buffer,bytestowrite,buffer,0,remaining); 
       numbytes = remaining; 
      } 

      line.drain(); 
     } 
     finally { // Always relinquish the resources we use 
      if (line != null) line.close(); 
      if (ain != null) ain.close(); 
     } 
    } 

и дать мне исключение

Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio 
input stream from input stream 
     at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source) 
     at test.PlaySoundStream.streamSampledAudio(PlaySoundStream.java:40) 
     at test.PlaySoundStream.main(PlaySoundStream.java:21) 

может помочь мне решить исключение или сказать мне о прочь может передавать им от SHOUTcast

ответ

0

Стараюсь этот код для загрузки MP3 от пренадлежат, а затем вы можете играть звук

public class DownloadMP3 { 

      private boolean halt = false; 
     public static final String DEFAULT_HOST = "127.0.0.1"; 
     protected String host = "url"; 
     public static final int DEFAULT_PORT = 80; 
     protected int port = 8568; 
     public static final int DEFAULT_TOTAL_SIZE = 0; 
     protected long totalSize = 0L; 
     public static final int DEFAULT_CHUNK_SIZE = 0; 
     long chunkSize = 0L; 
     public static final String DEFAULT_OUTPUT_DIRECTORY = "."; 
     File outputDirectory = new File("D:\\"); 

     public static void main(String[ ] args) throws Exception { 
     DownloadMP3 d = new DownloadMP3(); 

     d.run(); 
     } 

     public void run() 
     { 
     Socket localSocket = null; 
     PrintWriter localPrintWriter = null; 
     BufferedInputStream localBufferedInputStream = null; 
     FileOutputStream localFileOutputStream = null; 
     try 
     { 
      writeMessage("Opening connection to " + this.host + ":" + this.port); 

      localSocket = new Socket(this.host, this.port); 
      localPrintWriter = new PrintWriter(localSocket.getOutputStream(), true); 
      localBufferedInputStream = new BufferedInputStream(localSocket.getInputStream()); 

      localPrintWriter.print("GET/HTTP/1.0\r\n\r\n"); 
      localPrintWriter.flush(); 

      byte[] arrayOfByte = new byte[1024]; 
      long l1 = 0L; 
      long l2 = 0L; 
      int i = 1; 
      File localFile = null; 

      writeMessage("Host contacted, waiting for response..."); 
      try 
      { 
      int k = 0; 
      int m; 

      writeMessage("Recieving Data...."); 
      int j; 
      while ((j = localBufferedInputStream.read(arrayOfByte)) != -1) { 
       if ((localFileOutputStream == null) || ((this.chunkSize > 0L) && (l2 + j >= this.chunkSize))) { 
       m = findSync(arrayOfByte, 0, j); 
       if (m == -1) { 
        m = j; 
       } 
       if (localFileOutputStream != null) 
       { 
        localFileOutputStream.write(arrayOfByte, 0, m); 
       } 

       if (localFileOutputStream != null) { 
        localFileOutputStream.close(); 
       } 

       while ((localFile = new File(this.outputDirectory, this.host + '-' + this.port + '-' + formatFileNum(i++) + ".mp3")).exists()); 
       writeMessage("Saving to file: " + localFile); 

       localFileOutputStream = new FileOutputStream(localFile); 
       l2 = 0L; 

       localFileOutputStream.write(arrayOfByte, m, j - m); 
       l2 += j - m; 
       } else { 
       localFileOutputStream.write(arrayOfByte, 0, j); 
       l2 += j; 
       } 

       if ((this.totalSize > 0L) && (l1 >= this.totalSize)) { 
       writeMessage("Capture completed successfully."); 

       if (this.halt) { 
       writeMessage("Capture interruted."); 

       } 
      } 
      writeErrorMessage("Connection closed by host."); 
      return; 
      } catch (IOException localIOException2) { 
      if (this.halt) 
       writeMessage("Capture interruted."); 
      else { 
       writeErrorMessage(localIOException2.getMessage()); 
      } 

      } finally { 
      if (localFileOutputStream != null) 
       localFileOutputStream.close(); 
      } 
     } 
     catch (UnknownHostException localUnknownHostException) { 
      writeErrorMessage("Unknown host: " + this.host); 

      return; 
     } catch (IOException localIOException1) { 
      writeErrorMessage("Could not connect to " + this.host + " on port " + this.port); 

     } 
     finally { 
      if (localPrintWriter != null) { 
      localPrintWriter.close(); 
      } 
      if (localBufferedInputStream != null) 
      try { 
       localBufferedInputStream.close(); 
      } 
      catch (IOException localIOException3) { 
      } 
      if (localSocket != null) 
      try { 
       localSocket.close(); 
      } 
      catch (IOException localIOException4) 
      { 
      } 
     } 
     } 

     private static int findSync(byte[] paramArrayOfByte, int paramInt1, int paramInt2) 
     { 
     for (int i = paramInt1; i < paramInt2 - 1; i++) { 
      if (((paramArrayOfByte[i] & 0xFF) == 255) && ((paramArrayOfByte[(i + 1)] & 0xE0) == 224)) { 
      return i; 
      } 
     } 
     return -1; 
     } 

     private static String formatFileNum(int paramInt) 
     { 
     if (paramInt < 10) 
      return "00" + paramInt; 
     if (paramInt < 100) { 
      return "0" + paramInt; 
     } 
     return "" + paramInt; 
     } 

     protected void writeMessage(String paramString) 
     { 

      System.out.println(paramString); 
     } 

     protected void writeErrorMessage(String paramString) 
     { 

      System.err.println(paramString); 
     } 
}