1

I working on a program that both plays a sound when you click a button in an applet and in an application but I keep receiving an error while in the application portion of the program.

This is the error I get: sample.mp3 (The system cannot find the file specified) but I clearly have it in my project.

public class project extends JApplet {

public void init() {
    add(new AppletOrApplicationMainComponent());
}

public static void main(String args[]) {
    JFrame frame = new JFrame("");
    frame.getContentPane().add(new AppletOrApplicationMainComponent());
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
}
}

class AppletOrApplicationMainComponent extends JPanel {

public AppletOrApplicationMainComponent() {

    super(new BorderLayout());

    JButton button = new JButton("Play");
    button.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            Play();
        }
    });
    add(button, BorderLayout.SOUTH);
}

private void Play(){

    try{

        File file = new File("sample.mp3");
        FileInputStream fis = new FileInputStream(file);
        BufferedInputStream bis = new BufferedInputStream(fis);
        Player player = new Player(bis);
        player.play();

    }catch(Exception e){ 
        e.printStackTrace();
    }
}
}
Zombo
  • 1
  • 62
  • 391
  • 407
  • 2
    Embed the mp3 within the application (allowing it to be included within the Jar file) and then simply use `Class#getResource` or `Class#getResourceAsStream` to get a reference to it – MadProgrammer Aug 09 '15 at 02:59
  • Are you trying to run it in a jar or in your IDE? – durron597 Aug 09 '15 at 03:31
  • *"keep receiving an error.."* ..what error? Always copy/paste error and exception output! `Player player = new Player(bis);` and what is a `Player`? I don't see it in the Java 8 docs. For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/) (include the imports). – Andrew Thompson Aug 09 '15 at 03:33
  • 1
    Note: `File file = new File("sample.mp3");` Application resources, applets and files mix like oil and water (very poorly). Applet resources must be fetched by URL, as perhaps obtained via @MadProgrammer, first suggestion of `Class#getResource`. When it comes to Java Sound (at least) I would avoid the 2nd form that produces an `InputStream` because Java Sound needs a repositionable input stream and the one returned from `getResourceAsStream` usually isn't. – Andrew Thompson Aug 09 '15 at 03:39
  • 2
    @AndrewThompson That's true. I did see solution for this, but I can't remember what it is :P – MadProgrammer Aug 09 '15 at 03:48
  • @MadProgrammer Are you referring to.. *"Java Sound needs a repositionable input stream and the one returned from `getResourceAsStream` usually isn't."*? If so, note that I deliberately did not explain for two reasons. 1) I have no evidence yet that the `Player` is based on Java Sound (there were hidden `sun` package applet sound classes that in some ways surpassed Java Sound - or at least they did). 2) The OP had actually implemented the fix in their file based code. The fix is to wrap the input stream in a **buffered** input stream. ;) – Andrew Thompson Aug 09 '15 at 03:58
  • @AndrewThompson I think `Player` is from JavaZoom JLayer MP3 library, at a guess ;) – MadProgrammer Aug 09 '15 at 04:26
  • @MadProgrammer I don't like guessing. :) – Andrew Thompson Aug 09 '15 at 04:36

1 Answers1

1

In all that excitement, almost forgot to answer the question:

How do I play an mp3 file for both an application and applet?

Firstly, using URL to access a resource is compatible with both applet and application. The URL can be obtained from Class.getResource(String). For more details on using the method, see the embedded resource info. page.

As to playing an MP3 for which we have an URL, see the MP3 decoding support section of the Java Sound info. page. Basically it boils down to adding an MP3 SPI to the run-time class-path of the app.

Also note that the Clip convenience class mentioned in that page (and shown in the example) does not work with large files. From memory, it will hold at most 1 second of CD quality (stereophonic, 16 bit, 44.1 KHz) sound. For larger files you might use an AudioInputStream directly, reading the bytes and feeding them back out to the sound API that plays them. Either that or use BigClip, which caches the entire stream bytes in memory, much like Clip.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433