Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.70 KB | None | 0 0
  1.  
  2. import com.sun.corba.se.spi.ior.Writeable;
  3. import com.sun.xml.internal.bind.v2.model.core.ID;
  4. import javafx.embed.swing.SwingFXUtils;
  5. import javafx.scene.image.PixelWriter;
  6. import javafx.scene.media.Media;
  7.  
  8. import java.awt.*;
  9. import java.awt.image.BufferedImage;
  10. import java.awt.image.PixelGrabber;
  11. import java.io.ByteArrayInputStream;
  12. import java.io.File;
  13.  
  14. import com.mpatric.mp3agic.*;
  15.  
  16. import javafx.scene.image.Image;
  17.  
  18. import javax.imageio.ImageIO;
  19. import java.io.IOException;
  20. import java.nio.file.Paths;
  21. import java.util.Random;
  22. import java.util.Set;
  23. import javafx.scene.image.WritableImage;
  24. import org.farng.mp3.TagException;
  25.  
  26.  
  27. public class Jukebox {
  28.  
  29.     private AbstractStack<Album> albumStackHistory;
  30.     private AbstractStack<Album> albumStackNext;
  31.  
  32.     private AbstractSet<Media> songStackHistory;
  33.     private AbstractSet<Media> songStackNext;
  34.  
  35.     private AbstractSet<Album> allAlbums;
  36.  
  37.     private Album currentAlbum;
  38.     private Media currentSong;
  39.     private int currentSongTrack = 0;
  40.     public int songCounter;
  41.     public String currentSongName;
  42.  
  43. //construtor for jukebox
  44.     public Jukebox(String fileLocation) {
  45.  
  46.         //creates sets and stacks
  47.         allAlbums = new SetAsArray<Album>();
  48.         albumStackHistory = new StackAsLinkedList<Album>();
  49.         albumStackNext = new StackAsLinkedList<Album>();
  50.  
  51.         /* to be worked on*/
  52.         //songStackHistory = new AbstractSet<Media>();
  53.         //songStackNext = new AbstractSet<Media>();
  54.  
  55.         //creates a file from the location that was passed
  56.         File location = new File(fileLocation);
  57.         //runs checkfolder method passing the file
  58.         checkFolder(location);
  59.  
  60.         //randoms the first album to be played
  61.         currentAlbum = allAlbums.find(new Random().nextInt(allAlbums.size()));
  62.  
  63.         //adds all albums to the next album stack ready to be selected
  64.         for (int i = 0; i < allAlbums.size(); i++){
  65.             albumStackNext.push(allAlbums.find(i));
  66.         }
  67.         //runs the next song method starting the jukebox
  68.         nextSong();
  69.  
  70.     } //end of constructor
  71.  
  72.     public void checkFolder(File allFiles){
  73.  
  74.         //checks file, if file is a folder it will iterate through the folder until it finds a file.
  75.         //once a fle is found it will be sent into the foundSong method
  76.  
  77.         for (File file: allFiles.listFiles()){
  78.  
  79.             if(file.isDirectory()){
  80.                 checkFolder(file);
  81.             } else {
  82.                 foundSong(file);
  83.             }
  84.  
  85.         }
  86.  
  87.     }
  88.  
  89.  
  90.     public void nextSong(){
  91.  
  92.         //creates an int that finds the currents and adds 1 to it
  93.         int check = currentSongTrack + 1;
  94.  
  95.         //will check if the amount of songs in album is larger than the current song + 1 (check)
  96.         if(currentAlbum.getSongList().size() >= (check)) {
  97.             //songStackHistory.push(currentSong); to fix
  98.             //sets current song to the current song counter
  99.             currentSong = currentAlbum.getSongList().find(currentSongTrack);
  100.             //adds 1 to current song counter
  101.             currentSongTrack++;
  102.         }else{
  103.             //resets song counter to 0
  104.             currentSongTrack = 0;
  105.             //runs next album method
  106.             nextAlbum();
  107.  
  108.         }
  109.  
  110.     }
  111.  
  112.     public void previousSong(){
  113.  
  114.         int check = currentSongTrack + 1;
  115.         //will check if the amount of songs in album is smaller than the current song + 1 (check)
  116.         if(currentAlbum.getSongList().size() <= (check)) {
  117.             //songStackHistory.push(currentSong);
  118.             //sets currentsongtrack counter back 1
  119.             currentSongTrack -= 1;
  120.             //sets current song to currentsongtrack counter
  121.             currentSong = currentAlbum.getSongList().find(currentSongTrack);
  122.  
  123.         }else{
  124.             //resets song counter to 0
  125.             currentSongTrack = 0;
  126.             //runs previous album method
  127.             previousAlbum();
  128.  
  129.         }
  130.  
  131.     }
  132.  
  133.     /*  finds the previous album in the stack, if there is one.
  134.  
  135.         it then adds the current album to the album next stack
  136.  
  137.         and removes the previous album from the history album stack and sets that to the current album
  138.  
  139.         current song is then set to the first song in the current album
  140.  
  141.  
  142.      */
  143.  
  144.     private void previousAlbum() {
  145.  
  146.         if (currentAlbum != null){
  147.             albumStackNext.push(currentAlbum);
  148.         }
  149.  
  150.         currentAlbum = albumStackHistory.pop();
  151.  
  152.     }
  153.  
  154.     /*  finds the next album in the stack, if there is one.
  155.  
  156.         it then adds the current album to the album history stack
  157.  
  158.         and removes the next  album from the current album stack and sets it to current album
  159.  
  160.         current song is then set to the first song in the current album
  161.  
  162.      */
  163.  
  164.     private void nextAlbum() {
  165.  
  166.         if (currentAlbum != null){
  167.             albumStackHistory.push(currentAlbum);
  168.         }
  169.  
  170.         currentAlbum = albumStackNext.pop();
  171.  
  172.         currentSong = currentAlbum.getSongList().find(0);
  173.  
  174.     }
  175.  
  176.  
  177.     public void foundSong(File mp3) {
  178.  
  179.         //creates a string that holds the files path
  180.         String songName = mp3.getPath();
  181.         //creates a media with the file that has been found
  182.         Media media = new Media(new File(songName).toURI().toString());
  183.         try {
  184.  
  185.             //creates a mp3 file with the file
  186.             Mp3File mp3file = new Mp3File(mp3.getPath());
  187.  
  188.             String albumName;
  189.             String yearReleased;
  190.             String artist;
  191.  
  192.             //runs if statements to find if file has Id3v1 or Id3v2
  193.             //sets album name, artist and year released to info gotten from tags.
  194.             if (mp3file.hasId3v1Tag()) {
  195.  
  196.                 albumName = mp3file.getId3v1Tag().getAlbum();
  197.                 yearReleased = mp3file.getId3v1Tag().getYear();
  198.                 artist = mp3file.getId3v1Tag().getArtist();
  199.  
  200.             } else if(mp3file.hasId3v2Tag()) {
  201.  
  202.                 //if song has an ID3v2 create an id3v2 object
  203.                 ID3v2 id3v2Tag = mp3file.getId3v2Tag();
  204.  
  205.                 albumName = id3v2Tag.getAlbum();
  206.                 yearReleased = id3v2Tag.getYear();
  207.                 artist = id3v2Tag.getArtist();
  208.             }
  209.  
  210.             else {
  211.  
  212.                 albumName = "unknown";
  213.                 yearReleased = "unknown";
  214.                 artist = "unknown";
  215.             }
  216.  
  217.             Album album;
  218.  
  219.             if (mp3file.hasId3v2Tag()) {
  220.  
  221.                 ID3v2 id3v2Tag = mp3file.getId3v2Tag();
  222.  
  223.                 //creates a byte array and finds albumImage data
  224.                 byte[] imageData = id3v2Tag.getAlbumImage();
  225.                 //creates a buffered image from the data
  226.                 BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageData));
  227.                 //change buffered image to image
  228.                 Image image = SwingFXUtils.toFXImage(img, null);
  229.                 //creates a new album with the parameters
  230.                 album = new Album(albumName, yearReleased, artist, image);
  231.  
  232.                 //creates an album without albumart
  233.                 } else {
  234.                     album = new Album(albumName, yearReleased, artist);
  235.                 }
  236.  
  237.             //checks if album is already added to array
  238.             if (!allAlbums.contains(album)) {
  239.                 //if album doesnt exist create a new album
  240.                 album.getSongList().add(media);
  241.                 allAlbums.add(album);
  242.             } else {
  243.                 //if album already exists will add song to album
  244.                 Album oldAlbum = allAlbums.find(allAlbums.getIndex(album));
  245.                 oldAlbum.getSongList().add(media);
  246.             }
  247.  
  248.         } catch (InvalidDataException e) {
  249.             System.out.printf("Tag e");
  250.  
  251.     } catch (UnsupportedTagException e) {
  252.             System.out.printf("Tag e");
  253.         } catch (IOException e) {
  254.             System.out.printf("IO e");
  255.         }
  256.     } //end of song found method
  257.  
  258.  
  259.     public AbstractStack<Album> getAlbumStackHistory() {
  260.         return albumStackHistory;
  261.     }
  262.  
  263.     public AbstractStack<Album> getAlbumStackNext() {
  264.         return albumStackNext;
  265.     }
  266.  
  267.     /*public AbstractStack<Media> getSongStackHistory() {
  268.         return songStackHistory;
  269.     }
  270.  
  271.     public AbstractStack<Media> getSongStackNext() {
  272.         return songStackNext;
  273.     }*/
  274.  
  275.     public Media getCurrentSong() {
  276.         return currentSong;
  277.     }
  278.  
  279.     public int getSongCounter() {
  280.         return songCounter;
  281.     }
  282.  
  283.     public String getCurrentSongName() {
  284.         return currentSongName;
  285.     }
  286.  
  287.     public AbstractSet<Album> getAllAlbums() {return allAlbums;}
  288.  
  289.     public Album getCurrentAlbum() {
  290.         return currentAlbum;
  291.     }
  292.  
  293.  
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement