Problem bei der Nutzung von SwingWorker/ext. Bibliothek

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Problem bei der Nutzung von SwingWorker/ext. Bibliothek

    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 :(.

    Quellcode

    1. /**
    2. *
    3. * This method starts an anonymous SwingWorker class and work off the play list. It starts at the specific startTrack.
    4. *
    5. * Before it starts it will look if the last <code>PlaySwingWorker</code> is done to cancel it if it's running.
    6. *
    7. * @param startTrack the track number to start
    8. */
    9. public void playPlaylist(final int startTrack)
    10. {
    11. this.setPlaySwingWorker(new SwingWorker<Integer, Void>() {
    12. @Override
    13. protected java.lang.Integer doInBackground() throws Exception
    14. {
    15. System.out.println("Last track numb:" + getLastTrackNumb());
    16. setLastTrackNumb(startTrack);
    17. System.out.println("Last track numb:" + getLastTrackNumb());
    18. while(getLastTrackNumb() <= getPlaylist().size() && isStopped() == false)
    19. {
    20. System.out.println("neuer track!");
    21. try
    22. {
    23. setPlayer(new PlayerExtended(getPlaylist().get(getLastTrackNumb())));
    24. 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)!!!! */
    25. if(isStopped())
    26. {
    27. return null;
    28. }
    29. setLastTrackNumb(getLastTrackNumb() + 1);
    30. }
    31. catch (JavaLayerException e)
    32. {
    33. System.out.println("FAILED");
    34. e.printStackTrace();
    35. return null;
    36. }
    37. }
    38. return null;
    39. }
    40. @Override
    41. protected void done()
    42. {
    43. System.out.println("DONE");
    44. }
    45. });
    46. this.getPlaySwingWorker().execute();
    47. }
    48. /**
    49. *
    50. * This method plays the previous track of the play list by call playPlaylist() with
    51. * <code>last track number - 1</code>.
    52. *
    53. */
    54. public void playPrevTrack()
    55. {
    56. if(!this.isStarted()) return;
    57. this.stopPlaying();
    58. /* 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 */
    59. while(!this.getPlaySwingWorker().isDone())
    60. {
    61. try {
    62. Thread.sleep(100);
    63. } catch (InterruptedException e) {
    64. // TODO Auto-generated catch block
    65. e.printStackTrace();
    66. }
    67. }
    68. this.setStopped(false);
    69. this.setPlaySwingWorker(null);
    70. this.playPlaylist(getLastTrackNumb() - 1);
    71. }
    72. /**
    73. *
    74. * This method plays the next track of the play list by call playPlaylist() with
    75. * <code>last track number + 1</code>.
    76. *
    77. */
    78. public void playNextTrack()
    79. {
    80. if(!this.isStarted()) return;
    81. this.stopPlaying();
    82. /* 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 */
    83. while(!this.getPlaySwingWorker().isDone())
    84. {
    85. try {
    86. Thread.sleep(100);
    87. } catch (InterruptedException e) {
    88. // TODO Auto-generated catch block
    89. e.printStackTrace();
    90. }
    91. }
    92. this.setStopped(false);
    93. this.setPlaySwingWorker(null);
    94. this.playPlaylist(getLastTrackNumb() + 1);
    95. }
    96. public void stopPlaying()
    97. {
    98. if((this.getPlaySwingWorker() != null) && (this.getPlaySwingWorker().isDone() == false))
    99. {
    100. System.out.println("Stopping current track");
    101. this.setStopped(true);
    102. this.getPlayer().close();
    103. this.setPlayer(null);
    104. this.setPaused(false);
    105. }
    106. }
    Alles anzeigen