How to play audio recording from streaming?

Code :

String stringURL = "http://интернетРадио/радио_128"; // Тут рабочая ссылка InputStream is = new URL(stringURL).openStream(); AudioInputStream sound = null; try { sound = AudioSystem.getAudioInputStream(is); sound.read(); } catch(Exception e) { System.out.println("Error: " + e); } 

Mistake :

could not get audio

  • Maybe not enough codec? - smackmychi
  • @Burunduk, in what format does Internet radio broadcast by reference? - Nofate
  • 89.208.99.16:8088/zvezda_128 How to find out the format? - Burunduk
  • one
    @Burunduk, my link does not load. I can assume that there is MPEG , and AudioInputStream can only wav . ___ To play mp3, you can try JavaLayer . - Nofate

1 answer 1

Who is guilty?

As correctly noted by @Nofate ♦ , by reference stream is in MPEG format. In AudioSystem there is a getAudioFileTypes() method that returns (free translation of the documentation curve ):

Array of file types in which the system can record.

 AudioFileFormat.Type[] types = AudioSystem.getAudioFileTypes(); for (AudioFileFormat.Type type : types) { System.out.println(type); } 

In my case, the conclusion was:

 WAVE AU AIFF 

As you can see, there is no support for MPEG. This is the cause of the error.

If the author wrote the error completely

 Error: javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input stream 

then one would immediately see a UnsupportedAudioFileException .


What to do?

You can play MPEG from streaming, for example, using the JLayer library as follows:

 import java.io.InputStream; import java.net.URL; import javazoom.jl.player.*; public class Main { public static void main(String[] args) throws Exception { String stringURL = "http://89.208.99.16:8088/zvezda_128"; InputStream is = new URL(stringURL).openStream(); Player player = new Player(is); player.play(); } } 

Documentation - JLayer javadoc