Search code examples
javaeclipseaudiomp3playback

mp3transform | Playing (mp3's) on a Separate Thread


I am importing mp3transform into my java game. It is vital that I use mp3, and mp3transform works perfectly except for the fact it runs on the main thread, and therefore freezes the program. The already is a PlayerThread in the src, but it gives me a null pointer. I am new to the whole Java scene, and there seems to be no mp3transform help/docs D:

Any help would be appreciated. Thanks.


Solution

  •             Thread t1 = new Thread(new Runnable() {
                    public void run()
                    {
                        try{
                            Decoder decoder = new Decoder();
                                File file = new File("C:" + File.separator + "as.mp3");
                                FileInputStream in = new FileInputStream(file);
                                BufferedInputStream bin = new BufferedInputStream(in, 128 * 1024);
                                decoder.play(file.getName(), bin);
                                in.close();
    
                            decoder.stop();
                        }
                        catch(Exception exc){
                            exc.printStackTrace();
                            System.out.println("Failed to play the file.");
                        }
                    }});  
                    t1.start();