SongHandler.java 1.7 KB

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