SongHandler.java 2.0 KB

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