SongHandler.java 2.1 KB

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