SongHandler.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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.SortPLAYED;
  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 = 0;
  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. sql.update(songs);
  36. sort(new SortPLAYED());
  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. public boolean isPlaying(){
  59. return getProgress() > 0 ? true : false;
  60. }
  61. private void updatePlayer()
  62. {
  63. if(currentIndex < 0)
  64. currentIndex = songs.size() + currentIndex;
  65. if(currentIndex >= songs.size())
  66. currentIndex = currentIndex - songs.size();
  67. currentSong = songs.get(currentIndex);
  68. currentSongInstance = currentSong.getSongs().get(0);
  69. p.close();
  70. p.setClip(currentSong);
  71. }
  72. public long getProgress()
  73. {
  74. return p.getProgress();
  75. }
  76. public List<Song> getSongs()
  77. {
  78. return songs;
  79. }
  80. public Song getCurrentSong()
  81. {
  82. return currentSong;
  83. }
  84. public SongInstance getCurrentSongInstance()
  85. {
  86. return currentSongInstance;
  87. }
  88. public void close()
  89. {
  90. p.close();
  91. for(Song s : songs)
  92. {
  93. s.close();
  94. }
  95. }
  96. public void play()
  97. {
  98. p.play();
  99. }
  100. public void sort(Comparator<Song> sorter)
  101. {
  102. Collections.sort(songs, sorter);
  103. }
  104. }