简体   繁体   English

从Java Servlet中的磁盘进行音频流传输

[英]Audio Streaming from disk in java Servlets

To stream audio file I have implemented following code. 为了流音频文件,我实现了以下代码。 But i am getting Exception: 但我得到异常:

javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1170) javax.sound.sampled.UnsupportedAudioFileException:无法从javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1170)上的输入文件获取音频输入流

Can Any one help me please...... 谁能帮我......

    try {
        // From file

        System.out.println("hhhhhhhhhhhhhhhh");

        AudioInputStream stream = AudioSystem.getAudioInputStream(new File("C:\\track1.mp3"));

        System.out.println("stream created");

        AudioFormat format = stream.getFormat();
        if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) {

            System.out.println("in if");

            format = new AudioFormat(
                    AudioFormat.Encoding.PCM_SIGNED,
                    format.getSampleRate(),
                    format.getSampleSizeInBits()*2,
                    format.getChannels(),
                    format.getFrameSize()*2,
                    format.getFrameRate(),
                    true);        // big endian
            stream = AudioSystem.getAudioInputStream(format, stream);
        }

        // Create line
        SourceDataLine.Info info = new DataLine.Info(
            SourceDataLine.class, stream.getFormat(),
            ((int)stream.getFrameLength()*format.getFrameSize()));
        SourceDataLine line = (SourceDataLine) AudioSystem.getLine(info);
        line.open(stream.getFormat());
        line.start();

        // Continuously read and play chunks of audio
        int numRead = 0;
        byte[] buf = new byte[line.getBufferSize()];
        while ((numRead = stream.read(buf, 0, buf.length)) >= 0) {
            int offset = 0;
            while (offset < numRead) {
                offset += line.write(buf, offset, numRead-offset);
            }
        }
        line.drain();
        line.stop();
    }

That you're doing this job in a servlet class gives me the impression that your intent is to play the mp3 file whenever someone visits your website and that the visitor should hear this mp3 file. 您正在servlet类中完成此工作,给我的印象是,您的意图是每当有人访问您的网站时都播放mp3文件,并且访问者应该听到此mp3文件。

If true, I'm sorry to say, but you're approaching this entirely wrong. 如果为真,我很遗憾地说,但是您正在完全犯错。 Java servlet code runs in webserver machine and not in webbrowser machine. Java servlet代码在Web服务器计算机中运行,而不在Web浏览器计算机中运行。 Whenever someone visits your website, this way the mp3 file would only be played at the webserver machine. 每当有人访问您的网站时,这种mp3文件只能在网络服务器上播放。 This is usually a physically completely different machine which runs at the other side of the network connection and the visitor ain't ever going to hear the music. 这通常是一台物理上完全不同的机器,它在网络连接的另一端运行,并且访客永远不会听音乐。

You want to send the mp3 file raw (unmodified byte by byte) from webserver to the webbrowser without massaging it by some Java Audio API and instruct the webbrowser to play this file. 您想将mp3文件原始文件(逐字节未修改)从Web服务器发送到Web浏览器,而无需通过某些Java Audio API对其进行按摩,并指示Web浏览器播放此文件。 The easist way is to just drop the mp3 file in public webcontent (there where your HTML/JSP files also are) and use HTML <embed> tag to embed it in your HTML/JSP file. 简便的方法是将mp3文件拖放到公共Web内容(您的HTML / JSP文件也位于此处)中,然后使用HTML <embed>标记将其<embed>到HTML / JSP文件中。 The below example assumes the MP3 file to be in the same folder as the HTML/JSP file: 下面的示例假定MP3文件与HTML / JSP文件位于同一文件夹中:

<embed src="file.mp3" autostart="true"></embed>

That's all and this is supported in practically every browser and it will show a player as well. 仅此而已,几乎所有浏览器都支持此功能,并且还会显示一个播放器。

If the MP3 file is by business requirement stored outside public webcontent, then you may indeed need a servlet for this, but the servlet should do absolutely nothing more than getting an InputStream of it in some way and write it unmodified to the OutputStream of the HttpServletResponse the usual Java IO way. 如果MP3文件是根据业务需求存储在公共Web内容之外的,那么您可能确实为此需要一个servlet,但是该servlet绝对要做的不过是以某种方式获取它的InputStream并将其未经修改地写入到HttpServletResponseOutputStream 。通常的Java IO方法。 You only need to set the HTTP Content-Type header to audio/mpeg beforehand and if possible also the HTTP Content-Length header. 您只需要事先将HTTP Content-Type标头设置为audio/mpeg并且如果可能的话,还可以将HTTP Content-Length标头设置为。 Then point the src to the servlet's URL instead. 然后将src指向servlet的URL。

<embed src="mp3servlet" autostart="true"></embed>

Default java AudioInputStream does not support mp3 files. 默认java AudioInputStream不支持mp3文件。 You have to plug in MP3SPI to let it decode mp3. 您必须插入MP3SPI才能解码mp3。

ALso, what do you mean by streaming? 另外,您所说的流媒体是什么意思? This code will play the audio file, not stream it as in internet radio streaming. 此代码将播放音频文件,而不像在互联网广播流中那样流式传输它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM