在http协议上使用Java Media Framework流式传输音频

时间:2015-05-18 19:01:06

标签: java streaming jmf

我正在尝试通过http流式传输音频,可以通过VLC winamp或WMP打开。 我目前有代码通过rtp协议执行此操作,但不是http。只需将下面代码中的url更改为http即可。如何修改下面的代码以通过http流式传输?

public class MediaTransmitter {

        private MediaLocator mediaLocator = null;
        private DataSink dataSink = null;
        private Processor mediaProcessor = null;
        private static final Format[] FORMATS = new Format[]{new AudioFormat(AudioFormat.MPEG_RTP)};
        private static final ContentDescriptor CONTENT_DESCRIPTOR = new ContentDescriptor(ContentDescriptor.RAW_RTP);

        public MediaTransmitter(MediaLocator locator) {
            mediaLocator = locator;
        }

        public void startTransmitting() throws IOException {
            mediaProcessor.start();
            dataSink.open();
            dataSink.start();
        }

        public void stopTransmitting() throws IOException {
            dataSink.stop();
            dataSink.close();
            mediaProcessor.stop();
            mediaProcessor.close();
        }

        public void setDataSource(DataSource ds) throws IOException, NoProcessorException, CannotRealizeException, NoDataSinkException {
            mediaProcessor = Manager.createRealizedProcessor(new ProcessorModel(ds, FORMATS, CONTENT_DESCRIPTOR));
            dataSink = Manager.createDataSink(mediaProcessor.getDataOutput(), mediaLocator);
        }



        public static void main(String[] args) {

            try {
                MediaLocator locator = new MediaLocator("rtp://MY_PUBLIC_IP_HERE:10000/audio");
                MediaTransmitter transmitter = new MediaTransmitter(locator);
                System.out.println("-> Created media locator: '" + locator + "'");

                File mediaFile = new File("D:\\data\\audio\\posters.mp3");
                DataSource source = Manager.createDataSource(new MediaLocator(mediaFile.toURL()));
                System.out.println("-> Created data source: '" + mediaFile.getAbsolutePath() + "'");

                // set the data source.
                transmitter.setDataSource(source);
                System.out.println("-> Set the data source on the transmitter");

                // start transmitting the file over the network.
                transmitter.startTransmitting();
                System.out.println("-> Transmitting...");
                System.out.println("   Press the Enter key to exit");

                // wait for the user to press Enter to proceed and exit.
                System.in.read();
                System.out.println("-> Exiting");
                transmitter.stopTransmitting();
            } catch (Throwable t) {
                t.printStackTrace();
            }

            System.exit(0);
        }
    }

1 个答案:

答案 0 :(得分:1)

事实证明RTP是推送协议,因此在VLC中使用相同的URL来打开流失败。但是,如果我在VLC中将rtp更改为http并将URL指定到端口(减去其余网址),则可以正常工作。