1

Java を使用して mp3 プレーヤーを開発しようとしています。いくつかのコードを試しましたが、エラーが多すぎました。コードに関するヒントを提供し、JMF の構成も手伝ってください。

4

1 に答える 1

1

mp3 はオープン ソースではないため、JMF はネイティブで mp3 をサポートしていません。

mp3 ファイルを再生したい場合は、jlayer、mp3spi、および tritonus ライブラリを使用してこれを行うことができます。

これらのライブラリについてさらに情報が必要な場合は、お知らせください。

以下のコードをご覧ください。3 つのライブラリをビルド パスに追加すると、このコードが機能しました。これがあなたを助けることを願っています

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);
        }
    }
 }
于 2013-07-02T09:59:23.720 に答える