Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import com.sun.corba.se.spi.ior.Writeable;
- import com.sun.xml.internal.bind.v2.model.core.ID;
- import javafx.embed.swing.SwingFXUtils;
- import javafx.scene.image.PixelWriter;
- import javafx.scene.media.Media;
- import java.awt.*;
- import java.awt.image.BufferedImage;
- import java.awt.image.PixelGrabber;
- import java.io.ByteArrayInputStream;
- import java.io.File;
- import com.mpatric.mp3agic.*;
- import javafx.scene.image.Image;
- import javax.imageio.ImageIO;
- import java.io.IOException;
- import java.nio.file.Paths;
- import java.util.Random;
- import java.util.Set;
- import javafx.scene.image.WritableImage;
- import org.farng.mp3.TagException;
- public class Jukebox {
- private AbstractStack<Album> albumStackHistory;
- private AbstractStack<Album> albumStackNext;
- private AbstractSet<Media> songStackHistory;
- private AbstractSet<Media> songStackNext;
- private AbstractSet<Album> allAlbums;
- private Album currentAlbum;
- private Media currentSong;
- private int currentSongTrack = 0;
- public int songCounter;
- public String currentSongName;
- //construtor for jukebox
- public Jukebox(String fileLocation) {
- //creates sets and stacks
- allAlbums = new SetAsArray<Album>();
- albumStackHistory = new StackAsLinkedList<Album>();
- albumStackNext = new StackAsLinkedList<Album>();
- /* to be worked on*/
- //songStackHistory = new AbstractSet<Media>();
- //songStackNext = new AbstractSet<Media>();
- //creates a file from the location that was passed
- File location = new File(fileLocation);
- //runs checkfolder method passing the file
- checkFolder(location);
- //randoms the first album to be played
- currentAlbum = allAlbums.find(new Random().nextInt(allAlbums.size()));
- //adds all albums to the next album stack ready to be selected
- for (int i = 0; i < allAlbums.size(); i++){
- albumStackNext.push(allAlbums.find(i));
- }
- //runs the next song method starting the jukebox
- nextSong();
- } //end of constructor
- public void checkFolder(File allFiles){
- //checks file, if file is a folder it will iterate through the folder until it finds a file.
- //once a fle is found it will be sent into the foundSong method
- for (File file: allFiles.listFiles()){
- if(file.isDirectory()){
- checkFolder(file);
- } else {
- foundSong(file);
- }
- }
- }
- public void nextSong(){
- //creates an int that finds the currents and adds 1 to it
- int check = currentSongTrack + 1;
- //will check if the amount of songs in album is larger than the current song + 1 (check)
- if(currentAlbum.getSongList().size() >= (check)) {
- //songStackHistory.push(currentSong); to fix
- //sets current song to the current song counter
- currentSong = currentAlbum.getSongList().find(currentSongTrack);
- //adds 1 to current song counter
- currentSongTrack++;
- }else{
- //resets song counter to 0
- currentSongTrack = 0;
- //runs next album method
- nextAlbum();
- }
- }
- public void previousSong(){
- int check = currentSongTrack + 1;
- //will check if the amount of songs in album is smaller than the current song + 1 (check)
- if(currentAlbum.getSongList().size() <= (check)) {
- //songStackHistory.push(currentSong);
- //sets currentsongtrack counter back 1
- currentSongTrack -= 1;
- //sets current song to currentsongtrack counter
- currentSong = currentAlbum.getSongList().find(currentSongTrack);
- }else{
- //resets song counter to 0
- currentSongTrack = 0;
- //runs previous album method
- previousAlbum();
- }
- }
- /* finds the previous album in the stack, if there is one.
- it then adds the current album to the album next stack
- and removes the previous album from the history album stack and sets that to the current album
- current song is then set to the first song in the current album
- */
- private void previousAlbum() {
- if (currentAlbum != null){
- albumStackNext.push(currentAlbum);
- }
- currentAlbum = albumStackHistory.pop();
- }
- /* finds the next album in the stack, if there is one.
- it then adds the current album to the album history stack
- and removes the next album from the current album stack and sets it to current album
- current song is then set to the first song in the current album
- */
- private void nextAlbum() {
- if (currentAlbum != null){
- albumStackHistory.push(currentAlbum);
- }
- currentAlbum = albumStackNext.pop();
- currentSong = currentAlbum.getSongList().find(0);
- }
- public void foundSong(File mp3) {
- //creates a string that holds the files path
- String songName = mp3.getPath();
- //creates a media with the file that has been found
- Media media = new Media(new File(songName).toURI().toString());
- try {
- //creates a mp3 file with the file
- Mp3File mp3file = new Mp3File(mp3.getPath());
- String albumName;
- String yearReleased;
- String artist;
- //runs if statements to find if file has Id3v1 or Id3v2
- //sets album name, artist and year released to info gotten from tags.
- if (mp3file.hasId3v1Tag()) {
- albumName = mp3file.getId3v1Tag().getAlbum();
- yearReleased = mp3file.getId3v1Tag().getYear();
- artist = mp3file.getId3v1Tag().getArtist();
- } else if(mp3file.hasId3v2Tag()) {
- //if song has an ID3v2 create an id3v2 object
- ID3v2 id3v2Tag = mp3file.getId3v2Tag();
- albumName = id3v2Tag.getAlbum();
- yearReleased = id3v2Tag.getYear();
- artist = id3v2Tag.getArtist();
- }
- else {
- albumName = "unknown";
- yearReleased = "unknown";
- artist = "unknown";
- }
- Album album;
- if (mp3file.hasId3v2Tag()) {
- ID3v2 id3v2Tag = mp3file.getId3v2Tag();
- //creates a byte array and finds albumImage data
- byte[] imageData = id3v2Tag.getAlbumImage();
- //creates a buffered image from the data
- BufferedImage img = ImageIO.read(new ByteArrayInputStream(imageData));
- //change buffered image to image
- Image image = SwingFXUtils.toFXImage(img, null);
- //creates a new album with the parameters
- album = new Album(albumName, yearReleased, artist, image);
- //creates an album without albumart
- } else {
- album = new Album(albumName, yearReleased, artist);
- }
- //checks if album is already added to array
- if (!allAlbums.contains(album)) {
- //if album doesnt exist create a new album
- album.getSongList().add(media);
- allAlbums.add(album);
- } else {
- //if album already exists will add song to album
- Album oldAlbum = allAlbums.find(allAlbums.getIndex(album));
- oldAlbum.getSongList().add(media);
- }
- } catch (InvalidDataException e) {
- System.out.printf("Tag e");
- } catch (UnsupportedTagException e) {
- System.out.printf("Tag e");
- } catch (IOException e) {
- System.out.printf("IO e");
- }
- } //end of song found method
- public AbstractStack<Album> getAlbumStackHistory() {
- return albumStackHistory;
- }
- public AbstractStack<Album> getAlbumStackNext() {
- return albumStackNext;
- }
- /*public AbstractStack<Media> getSongStackHistory() {
- return songStackHistory;
- }
- public AbstractStack<Media> getSongStackNext() {
- return songStackNext;
- }*/
- public Media getCurrentSong() {
- return currentSong;
- }
- public int getSongCounter() {
- return songCounter;
- }
- public String getCurrentSongName() {
- return currentSongName;
- }
- public AbstractSet<Album> getAllAlbums() {return allAlbums;}
- public Album getCurrentAlbum() {
- return currentAlbum;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement