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) { }
}
}
}
}
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
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
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. –