Как правильно обнаружить, декодировать и воспроизвести радиопоток?

В настоящее время я пытаюсь написать приложение на Java, похожее на музыкальный автомат, которое может воспроизводить любой источник звука, но столкнулся с некоторыми трудностями при попытке воспроизвести радиопотоки.

Для воспроизведения я использую JLayer из JavaZoom, который отлично работает, пока цель является прямой медиафайл или прямой медиапоток (я прекрасно могу воспроизводить PCM, MP3 и OGG). Однако я сталкиваюсь с трудностями при попытке воспроизвести радиопотоки, которые либо содержат предварительные мультимедийные данные, такие как файл m3u/pls (что я мог бы исправить, предварительно добавив обнаружение), либо данные, которые передаются через порт 80, в то время как веб-страница существует на одно и то же местоположение и передаваемые носители зависят от типа запроса. В последнем случае всякий раз, когда я пытаюсь передать мультимедиа, вместо этого я получаю данные HTML.

Пример ссылки на поток, который скрыт за веб-страницей: http://stream.tn-media.de:8030
Это можно воспроизвести в VLC, но если вы поместите его в браузер или мое приложение, вы получите файл HTML.

Здесь:

  • Готовое бесплатное решение, которое я мог бы использовать вместо JLayer? Предпочтительно с открытым исходным кодом, чтобы я мог его изучить?
  • Учебник, который может помочь мне написать решение самостоятельно?
  • Или кто-нибудь может привести пример того, как правильно обнаруживать/запрашивать медиапоток?

Заранее спасибо!


person TwoThe    schedule 07.07.2014    source источник
comment
А как насчет MP3SPI? См. это для примера потоковой передачи.   -  person S.Pols    schedule 15.07.2014
comment
Я добавил пример радиопотока, который я не могу воспроизвести.   -  person TwoThe    schedule 16.07.2014


Ответы (3)


import java.io.*;
import java.net.*;
import javax.sound.sampled.*;
import javax.sound.midi.*;

/**
 * This class plays sounds streaming from a URL: it does not have to preload
 * the entire sound into memory before playing it. It is a command-line
 * application with no gui. It includes code to convert ULAW and ALAW
 * audio formats to PCM so they can be played. Use the -m command-line option
 * before MIDI files.
 */

public class PlaySoundStream {
    // Create a URL from the command-line argument and pass it to the 
    // right static method depending on the presence of the -m (MIDI) option.
    public static void main(String[  ] args) throws Exception {
        if (args[0].equals("-m")) streamMidiSequence(new URL(args[1]));
        else streamSampledAudio(new URL(args[0]));

        // Exit explicitly.
        // This is needed because the audio system starts background threads.
        System.exit(0);
    }

    /** Read sampled audio data from the specified URL and play it */
    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 {
            // Get an audio input stream from the URL
            ain=AudioSystem.getAudioInputStream(url);

            // Get information about the format of the stream
            AudioFormat format = ain.getFormat( );
            DataLine.Info info=new DataLine.Info(SourceDataLine.class,format);

            // If the format is not supported directly (i.e. if it is not PCM
            // encoded), then try to transcode it to PCM.
            if (!AudioSystem.isLineSupported(info)) {
                // This is the PCM format we want to transcode to.
                // The parameters here are audio format details that you
                // shouldn't need to understand for casual use.
                AudioFormat pcm =
                    new AudioFormat(format.getSampleRate( ), 16,
                                    format.getChannels( ), true, false);

                // Get a wrapper stream around the input stream that does the
                // transcoding for us.
                ain = AudioSystem.getAudioInputStream(pcm, ain);

                // Update the format and info variables for the transcoded data
                format = ain.getFormat( ); 
                info = new DataLine.Info(SourceDataLine.class, format);
            }

            // Open the line through which we'll play the streaming audio.
            line = (SourceDataLine) AudioSystem.getLine(info);
            line.open(format);  

            // Allocate a buffer for reading from the input stream and writing
            // to the line.  Make it large enough to hold 4k audio frames.
            // Note that the SourceDataLine also has its own internal buffer.
            int framesize = format.getFrameSize( );
            byte[  ] buffer = new byte[4 * 1024 * framesize]; // the buffer
            int numbytes = 0;                               // how many bytes

            // We haven't started the line yet.
            boolean started = false;

            for(;;) {  // We'll exit the loop when we reach the end of stream
                // First, read some bytes from the input stream.
                int bytesread=ain.read(buffer,numbytes,buffer.length-numbytes);
                // If there were no more bytes to read, we're done.
                if (bytesread == -1) break;
                numbytes += bytesread;

                // Now that we've got some audio data to write to the line,
                // start the line, so it will play that data as we write it.
                if (!started) {
                    line.start( );
                    started = true;
                }

                // We must write bytes to the line in an integer multiple of
                // the framesize.  So figure out how many bytes we'll write.
                int bytestowrite = (numbytes/framesize)*framesize;

                // Now write the bytes. The line will buffer them and play
                // them. This call will block until all bytes are written.
                line.write(buffer, 0, bytestowrite);

                // If we didn't have an integer multiple of the frame size, 
                // then copy the remaining bytes to the start of the buffer.
                int remaining = numbytes - bytestowrite;
                if (remaining > 0)
                    System.arraycopy(buffer,bytestowrite,buffer,0,remaining);
                numbytes = remaining;
            }

            // Now block until all buffered sound finishes playing.
            line.drain( );
        }
        finally { // Always relinquish the resources we use
            if (line != null) line.close( );
            if (ain != null) ain.close( );
        }
    }

    // A MIDI protocol constant that isn't defined by javax.sound.midi
    public static final int END_OF_TRACK = 47;

    /* MIDI or RMF data from the specified URL and play it */
    public static void streamMidiSequence(URL url)
        throws IOException, InvalidMidiDataException, MidiUnavailableException
    {
        Sequencer sequencer=null;     // Converts a Sequence to MIDI events
        Synthesizer synthesizer=null; // Plays notes in response to MIDI events

        try {
            // Create, open, and connect a Sequencer and Synthesizer
            // They are closed in the finally block at the end of this method.
            sequencer = MidiSystem.getSequencer( );
            sequencer.open( );  
            synthesizer = MidiSystem.getSynthesizer( );
            synthesizer.open( );
            sequencer.getTransmitter( ).setReceiver(synthesizer.getReceiver( ));

            // Specify the InputStream to stream the sequence from
            sequencer.setSequence(url.openStream( ));  

            // This is an arbitrary object used with wait and notify to 
            // prevent the method from returning before the music finishes
            final Object lock = new Object( );

            // Register a listener to make the method exit when the stream is 
            // done. See Object.wait( ) and Object.notify( )
            sequencer.addMetaEventListener(new MetaEventListener( ) {
                    public void meta(MetaMessage e) {
                        if (e.getType( ) == END_OF_TRACK) {
                            synchronized(lock) { 
                                lock.notify( );
                            }
                        }
                    }
                });

            // Start playing the music
            sequencer.start( );

            // Now block until the listener above notifies us that we're done.
            synchronized(lock) {
                while(sequencer.isRunning( )) {
                    try { lock.wait( ); } catch(InterruptedException e) {  }
                }
            }
        }
        finally {
            // Always relinquish the sequencer, so others can use it.
            if (sequencer != null) sequencer.close( );
            if (synthesizer != null) synthesizer.close( );
        }
    }
}

Я использовал этот фрагмент кода в одном из своих проектов, связанных с потоковой передачей аудио, и он работал нормально.

Кроме того, вы можете увидеть похожие примеры здесь: Пример Java Audio

person Mahder    schedule 15.07.2014
comment
Это примерно то, что я сейчас реализовал плюс добавление потоковой передачи MP3 и OGG благодаря JLayer, но ни моя, ни ваша не умеет воспроизводить радиопотоки. - person TwoThe; 16.07.2014

Просто прочитайте javadoc AudioSystem дайте мне идею.

Есть и другая подпись для getAudioInputStream: вы можете дать ему InputStream вместо URL.

Итак, попробуйте самостоятельно получить входной поток и добавить нужные заголовки, чтобы вы получили поток вместо html-контента:

URLConnection uc = url.openConnection();
uc.setRequestProperty("<header name here>", "<header value here>");

InputStream in = uc.getInputStream();

ain=AudioSystem.getAudioInputStream(in);

Надеюсь, это поможет.

person fluminis    schedule 18.07.2014

Я знаю, что этот ответ приходит поздно, но у меня была та же проблема: я хотел воспроизводить аудио MP3 и AAC, а также хотел, чтобы пользователь вставлял ссылки PLS/M3U. Вот что я сделал:
Сначала я попытался проанализировать тип, используя простое имя файла:

import de.webradio.enumerations.FileExtension;

import java.net.URL;

public class FileExtensionParser {
    /**
     *Parses a file extension
     * @param filenameUrl the url
     * @return the filename. if filename cannot be determined by file extension, Apache Tika parses by live detection
     */
    public FileExtension parseFileExtension(URL filenameUrl) {
        String filename = filenameUrl.toString();
        if (filename.endsWith(".mp3")) {
            return FileExtension.MP3;
        } else if (filename.endsWith(".m3u") || filename.endsWith(".m3u8")) {
            return FileExtension.M3U;
        } else if (filename.endsWith(".aac")) {
            return FileExtension.AAC;
        } else if(filename.endsWith((".pls"))) {
            return FileExtension.PLS;
        }
        URLTypeParser parser = new URLTypeParser();
        return parser.parseByContentDetection(filenameUrl);
    }
}

Если это не удается, я использую Apache Tika для обнаружения в реальном времени:

public class URLTypeParser {


    /** This class uses Apache Tika to parse an URL using her content
     *
     * @param url the webstream url
     * @return the detected file encoding: MP3, AAC or unsupported
     */

    public FileExtension parseByContentDetection(URL url) {
        try {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream in = connection.getInputStream();
            BodyContentHandler handler = new BodyContentHandler();
            AudioParser parser = new AudioParser();
            Metadata metadata = new Metadata();
            parser.parse(in, handler, metadata);
            return parseMediaType(metadata);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TikaException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
        return FileExtension.UNSUPPORTED_TYPE;
    }

    private FileExtension parseMediaType(Metadata metadata) {
        String parsedMediaType = metadata.get("encoding");
        if (parsedMediaType.equalsIgnoreCase("aac")) {
            return FileExtension.AAC;
        } else if (parsedMediaType.equalsIgnoreCase("mpeg1l3")) {
            return FileExtension.MP3;
        }
        return FileExtension.UNSUPPORTED_TYPE;
    }

}

Это также решит проблему с HTML, так как метод будет возвращать FileExtension.UNSUPPORTED для содержимого HTML.
Я объединил эти классы с фабричным шаблоном, и он отлично работает. Живое обнаружение занимает всего около двух секунд.

Я не думаю, что это вам больше поможет, но поскольку я боролся почти три недели, я хотел дать рабочий ответ. Весь проект можно посмотреть на github: https://github.com/Seppl2202/webradio

person ItFreak    schedule 31.08.2018