SongHandler.java 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 = -1;
  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("Songs");
  26. songs = DirScanner.scanDirectories(dir);
  27. }
  28. public void next()
  29. {
  30. currentIndex++;
  31. updatePlayer();
  32. }
  33. public void previous()
  34. {
  35. currentIndex--;
  36. updatePlayer();
  37. }
  38. public void set(int i)
  39. {
  40. if(currentIndex != i)
  41. {
  42. currentIndex = i;
  43. updatePlayer();
  44. }
  45. }
  46. private void updatePlayer()
  47. {
  48. if(currentIndex < 0)
  49. currentIndex = songs.size() + currentIndex;
  50. currentIndex%=songs.size();
  51. currentSong = songs.get(currentIndex);
  52. p.stop();
  53. p.setClip(currentSong);
  54. }
  55. public long getProgress()
  56. {
  57. return p.getProgress();
  58. }
  59. public List<Song> getSongs()
  60. {
  61. return songs;
  62. }
  63. public Song getCurrentSong()
  64. {
  65. return currentSong;
  66. }
  67. public void close()
  68. {
  69. p.close();
  70. for(Song s : songs)
  71. {
  72. s.close();
  73. }
  74. }
  75. public void play()
  76. {
  77. p.play();
  78. }
  79. public void play(boolean b)
  80. {
  81. if(b)
  82. {
  83. p.play((int)currentSong.getSampleStart()*10000);
  84. }
  85. }
  86. public void pause()
  87. {
  88. p.pause();
  89. }
  90. public void stop()
  91. {
  92. p.stop();
  93. }
  94. }