SongHandler.java 2.1 KB

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