Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中播放Ogg文件?_Java_Ogg - Fatal编程技术网

如何在Java中播放Ogg文件?

如何在Java中播放Ogg文件?,java,ogg,Java,Ogg,我使用Vorbis SPI来播放Ogg文件,但我得到了这个例外 javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1189) at application.OggPlayer.pla

我使用Vorbis SPI来播放Ogg文件,但我得到了这个例外

    javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
    at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1189)
    at application.OggPlayer.play(OggPlayer.java:31)
    at application.OggPlayer.main(OggPlayer.java:23)
这里是文档链接

这是我的密码

public class OggPlayer {
    static OggPlayer player;
    public static void main(String[] args) {
        player = new OggPlayer ();
        player.play(System.getProperty("user.dir")+ "/myFile.ogg");
    }

    public void play(String filePath) {
        try
        {
            File file = new File(filePath);
            // Get AudioInputStream from given file.
            AudioInputStream in= AudioSystem.getAudioInputStream(file);
            AudioInputStream din = null;
            if (in != null)
            {
                AudioFormat baseFormat = in.getFormat();
                AudioFormat  decodedFormat = new AudioFormat(
                        AudioFormat.Encoding.PCM_SIGNED,
                        baseFormat.getSampleRate(),
                        16,
                        baseFormat.getChannels(),
                        baseFormat.getChannels() * 2,
                        baseFormat.getSampleRate(),
                        false);
                // Get AudioInputStream that will be decoded by underlying VorbisSPI
                din = AudioSystem.getAudioInputStream(decodedFormat, in);
                // Play now !
                rawplay(decodedFormat, din);
                in.close();
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private void rawplay(AudioFormat targetFormat,
            AudioInputStream din) throws IOException, LineUnavailableException
    {
        byte[] data = new byte[4096];
        SourceDataLine line = getLine(targetFormat);
        if (line != null)
        {
            // Start
            line.start();
            int nBytesRead = 0, nBytesWritten = 0;
            while (nBytesRead != -1)
            {
                nBytesRead = din.read(data, 0, data.length);
                if (nBytesRead != -1) nBytesWritten = line.write(data, 0, nBytesRead);
            }
            // Stop
            line.drain();
            line.stop();
            line.close();
            din.close();
        }
    }

    private SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException
    {
        SourceDataLine res = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
        res = (SourceDataLine) AudioSystem.getLine(info);
        res.open(audioFormat);
        return res;
    }
}

我的密码有错误吗?提前感谢。

未经允许,抄袭自:

/**
*解码当前数据包并将其发送到音频输出线路。
*/
私有无效数据包()
{
int样本;
//检查数据包是否为音频数据包等。
if(jorbisBlock.synthesis(joggPacket)==0)
{
//将块指定给DspState对象。
jorbisDspState.阻断素的合成(jorbisBlock);
}
//我们需要知道要处理多少样品。
整数范围;
/*
*获取PCM信息并对样本进行计数
*样本超过零。。。
*/
而((样本=jorbidspstate.synthesis_pcmout(pcmInfo,pcmIndex))
> 0)
{
//我们需要知道我们要处理多少样品。
if(样本32767)
{
数值=32767;
}
如果(值<-32768)
{
值=-32768;
}
/*
*如果值小于零,则按位或按
*32768(即10000000000000=10^15)。
*/
如果(值<0)值=值| 32768;
/*
*把我们的价值分成两部分,一部分和最后一部分
*一个字节,一个字节包含第一个字节。
*/
convertedBuffer[sampleIndex]=(字节)(值);
convertedBuffer[sampleIndex+1]=(字节)(值>>>8);
/*
*将样本索引向前移动两步(因为这就是
*我们一次获得的许多值)乘以通道数。
*/
sampleIndex+=2*(jorbisInfo.channels);
}
}
//将缓冲区写入音频输出线。
outputLine.write(convertedBuffer,0,2*jorbisInfo.channels
*范围);
//更新DspState对象。
合成读数(范围);
}
}

我建议您首先检查程序中的文件是否正确。为此,您可以打印文件路径,也可以打印哪一行引发异常?
/**
 *  Decodes the current packet and sends it to the audio output line.
 */
private void decodeCurrentPacket()
{
    int samples;

    // Check that the packet is a audio data packet etc.
    if(jorbisBlock.synthesis(joggPacket) == 0)
    {
        // Give the block to the DspState object.
        jorbisDspState.synthesis_blockin(jorbisBlock);
    }

    // We need to know how many samples to process.
    int range;

    /*
     * Get the PCM information and count the samples. And while these
     * samples are more than zero...
     */
    while((samples = jorbisDspState.synthesis_pcmout(pcmInfo, pcmIndex))
        > 0)
    {
        // We need to know for how many samples we are going to process.
        if(samples < convertedBufferSize)
        {
            range = samples;
        }
        else
        {
            range = convertedBufferSize;
        }

        // For each channel...
        for(int i = 0; i < jorbisInfo.channels; i++)
        {
            int sampleIndex = i * 2;

            // For every sample in our range...
            for(int j = 0; j < range; j++)
            {
                /*
                 * Get the PCM value for the channel at the correct
                 * position.
                 */
                int value = (int) (pcmInfo[0][i][pcmIndex[i] + j] * 32767);

                /*
                 * We make sure our value doesn't exceed or falls below
                 * +-32767.
                 */
                if(value > 32767)
                {
                    value = 32767;
                }
                if(value < -32768)
                {
                    value = -32768;
                }

                /*
                 * It the value is less than zero, we bitwise-or it with
                 * 32768 (which is 1000000000000000 = 10^15).
                 */
                if(value < 0) value = value | 32768;

                /*
                 * Take our value and split it into two, one with the last
                 * byte and one with the first byte.
                 */
                convertedBuffer[sampleIndex] = (byte) (value);
                convertedBuffer[sampleIndex + 1] = (byte) (value >>> 8);

                /*
                 * Move the sample index forward by two (since that's how
                 * many values we get at once) times the number of channels.
                 */
                sampleIndex += 2 * (jorbisInfo.channels);
            }
        }

        // Write the buffer to the audio output line.
        outputLine.write(convertedBuffer, 0, 2 * jorbisInfo.channels
            * range);

        // Update the DspState object.
        jorbisDspState.synthesis_read(range);
    }
}