我正在尝试制作一个基于Java框架的简单音乐播放器。我已经成功地这样做了,但是如果它只播放.wav文件。即使我将加载文件条件更改为.mp3,然后加载了mp3文件,它也会显示UnsupportedAudioFileException ..请帮助我进行纠正。
这是我的代码
请告诉我需要做的纠正才能停止在cmd中显示此错误。

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.sound.sampled.*;
import java.io.*;
import java.net.*;

public class MusicPlayer extends Thread implements ActionListener {

    JLabel label;
    AudioInputStream ai;
    Clip c;
    JComboBox playlist;
    JButton play, loop, stop, open;
    JFrame f;
    String dest, str;
    JTextField tf;
    URL u;
    File file;

    MusicPlayer() {
        f = new JFrame("Music Player");
        f.setLayout(null);
        f.setSize(620, 300);
        f.setVisible(true);
        tf = new JTextField();
        tf.setBounds(25, 50, 565, 40);
        tf.setFont(new Font("Monotype Corsiva", Font.BOLD, 20));
        f.add(tf);
        tf.setHorizontalAlignment(JTextField.RIGHT);
        play = new JButton("play");
        play.setBounds(100, 150, 120, 30);
        play.addActionListener(this);
        f.add(play);
        loop = new JButton("loop");
        loop.setBounds(300, 150, 120, 30);
        loop.addActionListener(this);
        f.add(loop);
        stop = new JButton("stop");
        stop.setBounds(450, 150, 120, 30);
        stop.addActionListener(this);
        f.add(stop);
        open = new JButton("open");
        open.setBounds(100, 200, 420, 30);
        open.addActionListener(this);
        f.add(open);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }

    public void actionPerformed(ActionEvent ae) {
        String dest;

        if (ae.getActionCommand().equals("open")) {
            FileDialog fd = new FileDialog(f, "Open Box", FileDialog.LOAD);
            fd.setSize(300, 300);
            fd.setVisible(true);
            String s1 = "wav";
            String sng = fd.getFile();
            dest = fd.getDirectory() + fd.getFile();
            if (sng.toLowerCase().endsWith(s1)) {
                tf.setText(sng);
                file = new File(dest);
            } else {
                JOptionPane.showMessageDialog(f, "Select a valid file format");
            }
            try {
                c = AudioSystem.getClip();
                file = new File(dest);
                ai = AudioSystem.getAudioInputStream(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (ae.getActionCommand().equals("play")) {
            try {
                ai = AudioSystem.getAudioInputStream(file);
                c.close();
                c = AudioSystem.getClip();
                c.open(ai);
                c.start();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (ae.getActionCommand().equals("loop")) {
            try {
                ai = AudioSystem.getAudioInputStream(file);
                c.close();
                c = AudioSystem.getClip();
                c.open(ai);
                c.loop(Clip.LOOP_CONTINUOUSLY);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (ae.getActionCommand().equals("stop")) {
            c.stop();
            c.close();

        }
    }

    public static void main(String args[]) {
        new MusicPlayer();
    }
}

最佳答案

默认情况下,不提供MP3实现。如果没有MP3 SPI的实现,则会抛出UnsupportedAudioFileException
尝试this plugin,它应该可以工作!

另外,this article非常好,并提供了有关如何入门的详细说明。

07-24 09:36
查看更多