SongHandler.java 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. }
  56. public long getProgress()
  57. {
  58. return p.getProgress();
  59. }
  60. public List<Song> getSongs()
  61. {
  62. return songs;
  63. }
  64. public Song getCurrentSong()
  65. {
  66. return currentSong;
  67. }
  68. public void close()
  69. {
  70. p.close();
  71. for(Song s : songs)
  72. {
  73. s.close();
  74. }
  75. }
  76. public void play()
  77. {
  78. p.play();
  79. }
  80. public void play(boolean b)
  81. {
  82. if(b)
  83. {
  84. p.play((int)currentSong.getSampleStart()*10000);
  85. }
  86. }
  87. public void pause()
  88. {
  89. p.pause();
  90. }
  91. public void stop()
  92. {
  93. p.stop();
  94. }
  95. }