2016-01-23 21 views
5

Tôi muốn nhận tệp âm thanh tại URL được cung cấp trong mã và phát tệp đó (Có định dạng mp3). Tôi đã xem qua một số câu hỏi Stack Overflow liên quan đến vấn đề này, và tất cả họ đều nói để có được mp3plugin.jar vì vậy tôi đã làm.Không thể nhận luồng đầu vào âm thanh từ luồng đầu vào

Trong Eclipse, tôi đã thêm nó làm jar bên ngoài (vì nó nằm bên trong thư mục Downloads của tôi, không chắc đó có phải là nơi tốt nhất cho nó) trong Configure Build Path hay không. Tôi chạy nó một lần nữa và nó vẫn đem lại cho tôi lỗi này:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at Starter.main(Starter.java:21)

Đây là mã:

public class Starter { 

    public static void main(String[] args) { 
     AudioInputStream din = null; 
     try { 
      URL url = new URL("http://c5.rbxcdn.com/2e6d33a5b3b1d8f250c395816ff9f145"); 
      HttpURLConnection httpcon = (HttpURLConnection) url.openConnection(); 
      InputStream bufferedIn = new BufferedInputStream(httpcon.getInputStream()); 
      AudioInputStream in = AudioSystem.getAudioInputStream(bufferedIn); 
      AudioFormat baseFormat = in.getFormat(); 
      AudioFormat decodedFormat = new AudioFormat(
        AudioFormat.Encoding.PCM_SIGNED, 
        baseFormat.getSampleRate(), 16, baseFormat.getChannels(), 
        baseFormat.getChannels() * 2, baseFormat.getSampleRate(), 
        false); 
      din = AudioSystem.getAudioInputStream(decodedFormat, in); 
      DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat); 
      SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info); 
      if(line != null) { 
       line.open(decodedFormat); 
       byte[] data = new byte[4096]; 
       // Start 
       line.start(); 

       int nBytesRead; 
       while ((nBytesRead = din.read(data, 0, data.length)) != -1) { 
        line.write(data, 0, nBytesRead); 
       } 
       // Stop 
       line.drain(); 
       line.stop(); 
       line.close(); 
       din.close(); 
      } 

     } 
     catch(Exception e) { 
      e.printStackTrace(); 
     } 
     finally { 
      if(din != null) { 
       try { din.close(); } catch(IOException e) { } 
      } 
     } 
    } 
} 
+1

Bạn đã bao giờ kiểm tra những gì bạn đang đọc? Bạn có thể đang xử lý trang lỗi html dưới dạng MP3. – Marged

+0

Khi tôi nhập url đó vào Chrome của mình, tôi đã thấy 2 dòng trong tab mạng. Nó có thể cần thêm một số tiêu đề để cung cấp cho bạn luồng thực tế – Gavriel

+0

Làm cách nào để tôi nhận được phần phương tiện truyền thông này? (Nó nói tài liệu và phương tiện truyền thông). Tôi đã nghe jSoup có thể giúp giải quyết một số xử lý html trong java. –

Trả lời

0

Bạn cần phải nhận http://www.javazoom.net/mp3spi/docs/doc1.9.4/javazoom/spi/mpeg/sampled/file/MpegAudioFileReader.html

tải các lọ. Tôi có đường dẫn lớp học của tôi là

.;C:\Vision\Audio\libs\vorbisspi1.0.3.jar;C:\Vision\Audio\libs\tritonus_share.jar;C:\Vision\Audio\libs\tritonus_remaining-0.3.6.jar;C:\Vision\Audio\libs\jorbis-0.0.15.jar;C:\Vision\Audio\libs\jogg-0.0.7.jar;C:\Vision\Audio\libs\jl1.0.jar;C:\Vision\Audio\libs\mp3spi1.9.4.jar; 

bạn có thể chỉ cần mp3spi1.9.4.jar - một số trong số này là dành cho các định dạng khác nhưng tôi không chắc chắn vì vậy tôi bao gồm tất cả.

Sau đó có chương trình sau đây

public AudioInputStream readMP3URL(String f) { 
AudioInputStream audioInputStream=null; 
AudioFormat targetFormat=null; 
try { 
    AudioInputStream in=null; 
    MpegAudioFileReader mp=new MpegAudioFileReader(); 
    in=mp.getAudioInputStream(new URL(f)); 
    AudioFormat baseFormat=in.getFormat(); 
targetFormat=new AudioFormat(
      AudioFormat.Encoding.PCM_SIGNED, 
      baseFormat.getSampleRate(), 
      16, 
      baseFormat.getChannels(), 
      baseFormat.getChannels() * 2, 
      baseFormat.getSampleRate(), 
      false); 
     audioInputStream=AudioSystem.getAudioInputStream(targetFormat, in); 
} 
catch(Exception ue) { System.out.println("\nUnsupported Audio"); } 
return audioInputStream; 
} 

public void readURL() { 
    int i, j, k=0, l, basicU=1024; 
    AudioFormat targetFormat=null; 
audioInputStream=readMP3URL("http://c5.rbxcdn.com/2e6d33a5b3b1d8f250c395816ff9f145"); 
    if(audioInputStream==null) System.out.println("null audiostream"); 
targetFormat=audioInputStream.getFormat(); 
byte[] data=new byte[basicU]; 
DataLine.Info dinfo=new DataLine.Info(SourceDataLine.class, targetFormat); 
SourceDataLine line=null; 
    try { 
    line=(SourceDataLine)AudioSystem.getLine(dinfo); 
    if(line!=null) { 
    line.open(targetFormat); 
    line.start(); 
     while((k=audioInputStream.read(data, 0, data.length))!=-1) { 
     line.write(data, 0, k); 
    } 
    line.stop(); 
    line.close(); 
    } 
} 
catch(Exception ex) { ex.printStackTrace(); System.out.println("audio problem "+ex); } 
} 
Các vấn đề liên quan