0

i m trying to make a simple java frame based music player. i have succeded in doing so, but if it plays only .wav files. even if i change the load file condition to .mp3 and then load up an mp3 file, it shows an UnsupportedAudioFileException.. please help me to rectify it. here is my code please tell me the rectifications needed to be done for it to stop showing this error in the 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();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Have you tried to use some other methods from the AudioSystem class ? Instead of streaming a file, may be an input stream ? Did you try to use this method : 'static AudioInputStream getAudioInputStream(AudioFormat targetFormat, AudioInputStream sourceStream) Obtains an audio input stream of the indicated format, by converting the provided audio input stream.' – happybuddha Mar 08 '13 at 19:34
  • And from the docs, UnsupportedAudioFileException - if the stream does not point to valid audio file data recognized by the system. Are you sure you are getting the correct handle ? Can you put a debug point and see if the path is correct ? – happybuddha Mar 08 '13 at 19:36
  • @happybuddha i dont exactly know how to use AudioInput stream method correctly... if u would b soo kind and help me out in that.. it would be great.. cn u frmt the code accordingly nd den pasete if dats nt a problem ? – Udit Raizada Mar 09 '13 at 06:08

2 Answers2

2

By default, MP3 implementation isn't provided. If you don't have an implementation of MP3 SPI, UnsupportedAudioFileException will be thrown. Try this plugin and it should work!

Also, this article is pretty good and has detailed instructions about how to get you started.

Joel
  • 7,401
  • 4
  • 52
  • 58
1

If you are using Java7, you can use the built-in Media and MediaPlayer classes to playback MP3s.

Take a look at Playing .mp3 and .wav in Java? for more details.

Community
  • 1
  • 1
Jason Braucht
  • 2,358
  • 19
  • 31