diff --git a/pom.xml b/pom.xml index a477d63..1d66620 100644 --- a/pom.xml +++ b/pom.xml @@ -35,7 +35,12 @@ com.alex1304.jdash jdash-client - 4.0.5 + 4.0.5 + + + javazoom + jlayer + 1.0.1 diff --git a/src/api/GetApiData.java b/src/api/GetApiData.java index 82efd9d..0711af6 100644 --- a/src/api/GetApiData.java +++ b/src/api/GetApiData.java @@ -5,13 +5,22 @@ import jdash.common.entity.GDLevel; public class GetApiData { - public String getGDSongID(int id) { + public String getSongURL(int id) { GDClient client = GDClient.create(); - GDLevel level = client.findLevelById(id).block(); - String songID = level.song().get().downloadUrl() + ""; + String url = level.song().get().downloadUrl() + ""; + url = url.substring(url.indexOf("[") + 1, url.length() - 1); + + return url; + } + + public String getSongID(int id) { + GDClient client = GDClient.create(); + GDLevel level = client.findLevelById(id).block(); + + String songID = level.songId().get() + ""; songID = songID.substring(songID.indexOf("[") + 1, songID.length() - 1); return songID; diff --git a/src/filestructure/CreateFileStructure.java b/src/filestructure/CreateFileStructure.java index b2ba0b4..8d57765 100644 --- a/src/filestructure/CreateFileStructure.java +++ b/src/filestructure/CreateFileStructure.java @@ -9,6 +9,7 @@ public class CreateFileStructure { File file = new File("C:\\ExtremeDemonList\\levels"); File file2 = new File("C:\\ExtremeDemonList\\completed"); File file3 = new File("C:\\ExtremeDemonList\\index"); + File file4 = new File("C:\\ExtremeDemonList\\songs"); if(!file.isDirectory()) { file.mkdirs(); @@ -22,6 +23,10 @@ public class CreateFileStructure { file3.mkdirs(); } + if(!file4.exists()) { + file4.mkdirs(); + } + } } diff --git a/src/gui/VerifyInfo.java b/src/gui/VerifyInfo.java index 9bae0b6..0dbd2a6 100644 --- a/src/gui/VerifyInfo.java +++ b/src/gui/VerifyInfo.java @@ -2,6 +2,8 @@ package gui; import java.awt.Button; import java.awt.Image; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; import java.awt.image.BufferedImage; import java.io.IOException; import java.net.URL; @@ -12,6 +14,7 @@ import javax.swing.JFrame; import javax.swing.JLabel; import api.GetApiData; +import media.PlaySong; public class VerifyInfo { @@ -20,15 +23,36 @@ public class VerifyInfo { private JFrame frame; private JLabel ytthumbnail; private Button playsong; + private int currentSongId; // Instanzvariable für die ID + + private PlaySong player; + private GetApiData data; private VerifyInfo() { - frame = new JFrame("Verifikationsinfos"); + frame = new JFrame("Mehr Infos"); ytthumbnail = new JLabel(); + playsong = new Button("Song abspielen"); + + playsong.setBounds(90, 200, 200 ,30); frame.setSize(400, 300); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); frame.setLayout(null); + + player = new PlaySong(); + data = new GetApiData(); + + playsong.addActionListener(new ActionListener() { + @Override + public void actionPerformed(ActionEvent e) { + playSongAction(); + } + }); + } + + private void playSongAction() { + player.play(data.getSongURL(currentSongId), data.getSongID(currentSongId)); // Verwende die Instanzvariable für die ID } public static VerifyInfo getInstance() { @@ -42,6 +66,7 @@ public class VerifyInfo { frame.getContentPane().removeAll(); // Clear previous content ytthumbnail.setBounds(90, 70, 200, 110); + currentSongId = id; // Setze die Instanzvariable auf die aktuelle ID if (url != null && url.contains("v=")) { int startIndex = url.indexOf("v=") + 2; @@ -73,11 +98,7 @@ public class VerifyInfo { } frame.add(ytthumbnail); + frame.add(playsong); frame.setVisible(true); - - GetApiData data = new GetApiData(); - - System.out.println(data.getGDSongID(id)); - } } diff --git a/src/media/PlaySong.java b/src/media/PlaySong.java index 0f88215..55276a5 100644 --- a/src/media/PlaySong.java +++ b/src/media/PlaySong.java @@ -1,5 +1,96 @@ package media; -public class PlaySong { +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; +import javazoom.jl.decoder.JavaLayerException; +import javazoom.jl.player.advanced.AdvancedPlayer; + +public class PlaySong { + + public void play(String link, String id) { + checkExisting(id, link); + } + + private synchronized void checkExisting(String songId, String link) { + String path = "C:/ExtremeDemonList/songs/" + songId + ".mp3"; + File file = new File(path); + + if(file.exists()) { + playSong(path); + } else { + downloadSong(link, songId); + playSong(path); + System.out.println("nein"); + } + } + + private void playSong(String path) { + String filePath = path; // Passe den Pfad zu deiner MP3-Datei an + + try { + FileInputStream fis = new FileInputStream(filePath); + AdvancedPlayer player = new AdvancedPlayer(fis); + + // Starte das Abspielen in einem neuen Thread, damit der Hauptthread nicht blockiert wird + Thread playerThread = new Thread(() -> { + try { + player.play(); + } catch (JavaLayerException e) { + e.printStackTrace(); + } + }); + + playerThread.start(); + + // Gib dem Player Zeit, die MP3-Datei abzuspielen + Thread.sleep(10000); // Zum Beispiel 10 Sekunden abwarten + + // Beende den Player und schließe die FileInputStream + player.close(); + fis.close(); + } catch (FileNotFoundException | JavaLayerException | InterruptedException e) { + e.printStackTrace(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } + + private void downloadSong(String link, String id) { + try { + URL url = new URL(link); + HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(); + int responseCode = httpConn.getResponseCode(); + + if (responseCode == HttpURLConnection.HTTP_OK) { + InputStream inputStream = httpConn.getInputStream(); + String saveFilePath = "C:/ExtremeDemonList/songs/" + id + ".mp3"; + + FileOutputStream outputStream = new FileOutputStream(saveFilePath); + + int bytesRead; + byte[] buffer = new byte[4096]; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outputStream.write(buffer, 0, bytesRead); + } + + outputStream.close(); + inputStream.close(); + + } else { + System.out.println("Der Server hat mit dem Statuscode " + responseCode + " geantwortet."); + } + httpConn.disconnect(); + } catch (IOException e) { + e.printStackTrace(); + } + } } diff --git a/src/module-info.java b/src/module-info.java index 32ba37f..b577c5f 100644 --- a/src/module-info.java +++ b/src/module-info.java @@ -12,4 +12,5 @@ requires com.formdev.flatlaf; requires jdash.client; requires jdash.common; + requires jlayer; } \ No newline at end of file