繁体   English   中英

MP3播放器和JMF

[英]Mp3 player and JMF

我正在尝试使用java开发mp3播放器。我尝试了一些代码,但最终出现了太多错误。因此请向我提供有关代码的提示,并帮助我配置JMF?

JMF本身不支持mp3,因为mp3不是开源的。

如果要播放mp3文件,则可以使用jlayer,mp3spi和tritonus库执行此操作。

如果您需要有关这些库的更多信息,请告诉我。

请参见下面的代码。 将三个库添加到构建路径后,此代码为我工作。 希望这个能对您有所帮助

String mp3File = "path to mp3 file";

public void playMp3(String mp3File ) {
    AudioInputStream din = null;
    AudioInputStream in = null;
    try {
        File file = new File(mp3File);
        in = AudioSystem.getAudioInputStream(file);
        AudioFormat baseFormat = in.getFormat();
        AudioFormat decodedFormat = new AudioFormat(
                AudioFormat.Encoding.PCM_SIGNED,
                baseFormat.getSampleRate(), 16, baseFormat.getChannels(),
                baseFormat.getChannels() * 2, baseFormat.getSampleRate(),
                false);
        din = AudioSystem.getAudioInputStream(decodedFormat, in);
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, decodedFormat);
        line = (SourceDataLine) AudioSystem.getLine(info);

        if (line != null) {
            line.open(decodedFormat);
            byte[] data = new byte[4096];
            // Start
            line.start();

            int nBytesRead;
            while ((nBytesRead = din.read(data, 0, data.length)) != -1) {
                line.write(data, 0, nBytesRead);
                if (flag) {
                    break;
                }
            }
            line.drain();
            line.stop();
            line.close();
            din.close();
        }

    } catch (UnsupportedAudioFileException uafe) {
        JOptionPane.showMessageDialog(null, uafe.getMessage());
        logger.error(uafe);
    } catch (LineUnavailableException lue) {
        JOptionPane.showMessageDialog(null, lue.getMessage());
        logger.error(lue);
    } catch (IOException ioe) {
        JOptionPane.showMessageDialog(null, ioe.getMessage());
        logger.error(ioe);
    } finally {
        if (din != null) {
            try {
                din.close();
            } catch (IOException e) {
            }
        }
        try {
            in.close();
        } catch (IOException ex) {
            logger.error(ex);
        }
    }
 }

暂无
暂无

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

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