Hallo,
ich muss nochmal wegen einem Problem hier fragen. Ich habe eine Klasse mit der steuere ich die Nutzung von JavaLayer (so ne Bibliothek um MP3 Dateien abzuspielen). Mein Problem ist ich kann mehrfach hintereinander playPrevTrack() oder playNextTrack() aufrufen aber sobald ich dann das andere einmal starte verläuft der sich irgendwie in der SwingWorker Klasse bzw in der inneren while Schleife und es hagelt dann von Exceptions.
Das merkwürdige die Exceptions kommen nur von JavaLayer Bibliothek. Mich wunderts aber warum das mehrfache ausführen von EINER Methode funktioniert aber sobald die andere gestartet wird die eigentlich das selbe macht die die jeweils andere und nur +- 1 zu etwas zählt gibts Probleme.
Hier ist mal der Code, es wäre cool wenn da mal einer gucken kann ob ich vllt. davor was falsch mache :(.
Alles anzeigen
ich muss nochmal wegen einem Problem hier fragen. Ich habe eine Klasse mit der steuere ich die Nutzung von JavaLayer (so ne Bibliothek um MP3 Dateien abzuspielen). Mein Problem ist ich kann mehrfach hintereinander playPrevTrack() oder playNextTrack() aufrufen aber sobald ich dann das andere einmal starte verläuft der sich irgendwie in der SwingWorker Klasse bzw in der inneren while Schleife und es hagelt dann von Exceptions.
Das merkwürdige die Exceptions kommen nur von JavaLayer Bibliothek. Mich wunderts aber warum das mehrfache ausführen von EINER Methode funktioniert aber sobald die andere gestartet wird die eigentlich das selbe macht die die jeweils andere und nur +- 1 zu etwas zählt gibts Probleme.
Hier ist mal der Code, es wäre cool wenn da mal einer gucken kann ob ich vllt. davor was falsch mache :(.
Quellcode
- /**
- *
- * This method starts an anonymous SwingWorker class and work off the play list. It starts at the specific startTrack.
- *
- * Before it starts it will look if the last <code>PlaySwingWorker</code> is done to cancel it if it's running.
- *
- * @param startTrack the track number to start
- */
- public void playPlaylist(final int startTrack)
- {
- this.setPlaySwingWorker(new SwingWorker<Integer, Void>() {
- @Override
- protected java.lang.Integer doInBackground() throws Exception
- {
- System.out.println("Last track numb:" + getLastTrackNumb());
- setLastTrackNumb(startTrack);
- System.out.println("Last track numb:" + getLastTrackNumb());
- while(getLastTrackNumb() <= getPlaylist().size() && isStopped() == false)
- {
- System.out.println("neuer track!");
- try
- {
- setPlayer(new PlayerExtended(getPlaylist().get(getLastTrackNumb())));
- getPlayer().play(); /* HIER: An der Stelle fängt es dann an Exceptions zu werfen, aber halt nur wenn Next bzw. Prev gestartet wurde (und davor halt ausschließlich das andere)!!!! */
- if(isStopped())
- {
- return null;
- }
- setLastTrackNumb(getLastTrackNumb() + 1);
- }
- catch (JavaLayerException e)
- {
- System.out.println("FAILED");
- e.printStackTrace();
- return null;
- }
- }
- return null;
- }
- @Override
- protected void done()
- {
- System.out.println("DONE");
- }
- });
- this.getPlaySwingWorker().execute();
- }
- /**
- *
- * This method plays the previous track of the play list by call playPlaylist() with
- * <code>last track number - 1</code>.
- *
- */
- public void playPrevTrack()
- {
- if(!this.isStarted()) return;
- this.stopPlaying();
- /* because we have to wait until the old SwingWorker Thread is completely done by ITSELF we make a loop where I let the thread to sleep for some time */
- while(!this.getPlaySwingWorker().isDone())
- {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- this.setStopped(false);
- this.setPlaySwingWorker(null);
- this.playPlaylist(getLastTrackNumb() - 1);
- }
- /**
- *
- * This method plays the next track of the play list by call playPlaylist() with
- * <code>last track number + 1</code>.
- *
- */
- public void playNextTrack()
- {
- if(!this.isStarted()) return;
- this.stopPlaying();
- /* because we have to wait until the old SwingWorker Thread is completely done by ITSELF we make a loop where I let the thread to sleep for some time */
- while(!this.getPlaySwingWorker().isDone())
- {
- try {
- Thread.sleep(100);
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- this.setStopped(false);
- this.setPlaySwingWorker(null);
- this.playPlaylist(getLastTrackNumb() + 1);
- }
- public void stopPlaying()
- {
- if((this.getPlaySwingWorker() != null) && (this.getPlaySwingWorker().isDone() == false))
- {
- System.out.println("Stopping current track");
- this.setStopped(true);
- this.getPlayer().close();
- this.setPlayer(null);
- this.setPaused(false);
- }
- }