SongHandler.java 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package model;
  2. import java.io.File;
  3. import java.util.ArrayList;
  4. import java.util.List;
  5. import main.Window;
  6. import audio.AudioPlayer;
  7. import audio.Song;
  8. import audio.SongInstance;
  9. import audio.io.DirScanner;
  10. public class SongHandler {
  11. private List<Song> songs;
  12. private Song currentSong;
  13. private SongInstance currentSongInstance;
  14. private int currentIndex;
  15. private File dir;
  16. private AudioPlayer p;
  17. public SongHandler()
  18. {
  19. songs = new ArrayList<Song>();
  20. currentSong = null;
  21. currentSongInstance = null;
  22. currentIndex = 1;
  23. p = new AudioPlayer();
  24. if(Window.ON_RASP)
  25. dir = new File(System.getProperty( "user.home" ) + "/ColorStrike/Songs/");
  26. else
  27. dir = new File(System.getProperty( "user.home" ) + "/ColorStrike/Songs/");
  28. songs = DirScanner.scanDirectories(dir);
  29. System.out.println(songs.size());
  30. updatePlayer();
  31. }
  32. public void next()
  33. {
  34. currentIndex++;
  35. updatePlayer();
  36. }
  37. public void previous()
  38. {
  39. currentIndex--;
  40. updatePlayer();
  41. }
  42. public void set(int i)
  43. {
  44. currentIndex = i;
  45. updatePlayer();
  46. }
  47. public void set(SongInstance si)
  48. {
  49. currentSongInstance = si;
  50. }
  51. private void updatePlayer()
  52. {
  53. if(currentIndex < 0)
  54. currentIndex = songs.size() + currentIndex;
  55. if(currentIndex >= songs.size())
  56. currentIndex = currentIndex - songs.size();
  57. currentSong = songs.get(currentIndex);
  58. currentSongInstance = currentSong.getSongs().get(0);
  59. p.close();
  60. p.setClip(currentSong);
  61. }
  62. public long getProgress()
  63. {
  64. return p.getProgress();
  65. }
  66. public List<Song> getSongs()
  67. {
  68. return songs;
  69. }
  70. public Song getCurrentSong()
  71. {
  72. return currentSong;
  73. }
  74. public SongInstance getCurrentSongInstance()
  75. {
  76. return currentSongInstance;
  77. }
  78. public void close()
  79. {
  80. p.close();
  81. for(Song s : songs)
  82. {
  83. s.close();
  84. }
  85. }
  86. public void play()
  87. {
  88. p.play();
  89. }
  90. }