0

I am working on a Java project that involves playing mp3 files. I want my application to play the files from within the project so I have the music files stored in a folder called music which is in a source folder called resources. This is the code I have right now but when I run it I get a Bitstream errorcode 102. I can't seem to figure out what is wrong, any help? I am using the javazoom library (javazoom.jl.player.Player)

public void play() {
    try {
        InputStream stream = MP3.class.getClassLoader()
                .getResourceAsStream("/music/LoveStory.mp3");
        BufferedInputStream bis = new BufferedInputStream(stream);
        player = new Player(bis);
    } catch (Exception e) {
        System.out.println("Problem playing file " + filename);
        System.out.println(e);
    }

    // run in new thread to play in background
    new Thread() {
        public void run() {
            try {
                player.play();
            } catch (Exception e) {
                System.out.println(e);
            }
        }
    }.start();

}
MANOJ GOPI
  • 1,279
  • 10
  • 31
user4305589
  • 43
  • 1
  • 7

3 Answers3

0

Try closing the BufferedInputStream before setting player

bis.close();

I found a similar problem here.

Community
  • 1
  • 1
applecrusher
  • 5,508
  • 5
  • 39
  • 89
  • Thanks this seemed to make a difference but is still not quite right. Now a sound begins (not exactly like a buzzing but a light hum) as if the song is about to play but it still never does. I still get the same errorcode 102 – user4305589 Jan 10 '15 at 21:39
  • Which operating system are you using linux or mac? The reason I ask is because the music folder is according to the path in your root directory. This is a very odd place to put your music folder. Do you mean it to be there? – applecrusher Jan 11 '15 at 05:29
  • Do you mean "/music/LoveStory.mp3" to be filename? – applecrusher Jan 11 '15 at 05:37
  • I am using Mac. I have a source folder called resources and my music folder is inside this resources folder. And to answer the filename part sorry that was just an old part of cold I forgot to delete! – user4305589 Jan 12 '15 at 15:15
  • No problem. I just saw something else that said that the error could also be caused by the filepath so I figured I ask. Have you also tried closing the InputStream too before you put it into the BufferedInputStream? – applecrusher Jan 12 '15 at 23:44
  • Hmmm I tried that and it got rid of my errorcode 102 but now it just jumps to the catch statement @user1917363 – user4305589 Jan 14 '15 at 20:50
0

I actually discovered my problem and it is such a small thing I am quite embarrassed! It turns out when I removed the leading "/" in front of the path name my code ran fine. I believe this had to do with an absolute path vs a relative path.

user4305589
  • 43
  • 1
  • 7
0

Use the JavaFX environment:

package be.saleconix;

import java.net.URL;

import com.sun.javafx.application.PlatformImpl;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.util.Duration;

class AudioPlayer implements Runnable {
    private URL resource;
    private Media media;
    private MediaPlayer mediaPlayer;
    private boolean ready = false;
    private Thread t;
    private String threadname;
    private static Integer numberOfActiveThreads = 0;

    /**
     * Start a mediaplayer thread
     * @param filename
     * @param threadname
     * @throws NullPointerException
     * @throws InterruptedException
     */
    public AudioPlayer(String filename, String threadname) throws NullPointerException, InterruptedException {
        if (numberOfActiveThreads == 0) {
            PlatformImpl.startup(()->{}); // initialize JavaFX environment
            System.out.println("The JavaFX environment has been started");
        }
        this.threadname = threadname;
        resource = getClass().getResource("/" + filename);
        media = new Media(resource.toString());
        t = new Thread(this, threadname);
        t.start();
        numberOfActiveThreads ++;
    }

    public void run() {
        mediaPlayer = new MediaPlayer(media);
        mediaPlayer.setOnReady(() -> { // register a callback and let it deregister itself after execution
            System.out.println("The mediaplayer [" + threadname + "] has been started");
            ready = true;
            mediaPlayer.setOnReady(null);
        });
        while (!t.isInterrupted());
        System.out.println("The mediaplayer [" + threadname + "] has been terminated");
    }

    String getThreadname() {
        return threadname;
    }

    /**
     * Plays the sound from the start
     * @throws InterruptedException
     */
    void play() throws InterruptedException {
        play(0);
    }

    /**
     * Plays the sound at a specific position in ms
     * @throws InterruptedException
     * @throws IllegalThreadStateException
     */
    void play(double position) throws InterruptedException, IllegalThreadStateException {
        int i = 500;
        while (!ready && i > 0) {
            Thread.sleep(1);
            i--;
        }
        if (i > 0) {
            mediaPlayer.setStartTime(Duration.millis(position));
            if (mediaPlayer.getStatus() == MediaPlayer.Status.PLAYING) {
                mediaPlayer.setOnStopped(()->{
                    mediaPlayer.play();
                    mediaPlayer.setOnStopped(null);
                });
                mediaPlayer.stop();
            } else {
                mediaPlayer.play();
            }
        } else {
            throw new IllegalThreadStateException("The mediaplayer is not ready after 500ms timeout");
        }
    }

    /**
     * Stop the mediaplayer thread
     */
    void stop() {
        mediaPlayer.setOnReady(null);
        mediaPlayer.setOnStopped(null);
        mediaPlayer.stop();
        numberOfActiveThreads --;
        t.interrupt();
        if (numberOfActiveThreads == 0) {
            PlatformImpl.exit(); // exit the JavaFX environment
            System.out.println("The JavaFX environment has been exited");
        }
    }

}

public class Main {
    public static void main(String[] args) {
        try {
            AudioPlayer audioPlayerA = new AudioPlayer("test.mp3", "audioplayerA");

            System.out.println("Play audioplayerA from start");
            audioPlayerA.play();

            System.out.println("Waiting 10s ...");
            Thread.sleep(10000);

            System.out.println("Play audioplayerA at position 20s");
            audioPlayerA.play(20000);

            System.out.println("Waiting 5s ...");
            Thread.sleep(5000);

            AudioPlayer audioPlayerB = new AudioPlayer("test1.mp3", "audioplayerB");

            System.out.println("Play audioplayerB from start.");
            audioPlayerB.play();

            System.out.println("Waiting 20s ...");
            Thread.sleep(20000);

            System.out.println("Stopping audioplayerA");
            audioPlayerA.stop();

            System.out.println("Stopping audioplayerB");
            audioPlayerB.stop();

        } catch (NullPointerException e) {
            System.out.println("Cannot create audio player, check filename.");
        } catch (InterruptedException e) {
            System.out.println("Thread interrupted: " + e);
        } finally {
            System.out.println("Main thread has been exited");
        }
    }
}
Sam
  • 11
  • 4