SongHandler.java 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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" ) + "/Documents/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. if(currentIndex != i)
  45. {
  46. currentIndex = i;
  47. updatePlayer();
  48. }
  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.stop();
  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 play(boolean b)
  94. {
  95. if(b)
  96. {
  97. p.play((int)currentSong.getSampleStart()*10);
  98. }
  99. }
  100. public void pause()
  101. {
  102. p.pause();
  103. }
  104. public void stop()
  105. {
  106. p.stop();
  107. }
  108. }