From e58b48a3162539cb79ff495d6040ca6f53f069d6 Mon Sep 17 00:00:00 2001 From: said belhadj Date: Sat, 26 Jun 2021 01:58:15 +0200 Subject: Add the possibility to listen a song thanks to Audio System library --- src/musichub/business/AudioElement.java | 16 ++++++++++++++++ src/musichub/business/MusicHub.java | 13 +++++++++++++ src/musichub/business/Song.java | 11 +++++++++++ src/musichub/main/Main.java | 11 ++++++++++- 4 files changed, 50 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/musichub/business/AudioElement.java b/src/musichub/business/AudioElement.java index e0a686b..9294b49 100644 --- a/src/musichub/business/AudioElement.java +++ b/src/musichub/business/AudioElement.java @@ -3,8 +3,16 @@ package musichub.business; import org.w3c.dom.Document; import org.w3c.dom.Element; +import java.io.File; +import java.io.IOException; import java.util.UUID; +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; + public abstract class AudioElement { protected String title; protected String artist; @@ -84,5 +92,13 @@ public abstract class AudioElement { parentElement.appendChild(contentElement); } + + public void playSong() throws UnsupportedAudioFileException, IOException, LineUnavailableException { + File file = new File(this.content); + AudioInputStream audioStream = AudioSystem.getAudioInputStream(file); + Clip clip = AudioSystem.getClip(); + clip.open(audioStream); + clip.start(); + } } \ No newline at end of file diff --git a/src/musichub/business/MusicHub.java b/src/musichub/business/MusicHub.java index 38e2214..e138d84 100644 --- a/src/musichub/business/MusicHub.java +++ b/src/musichub/business/MusicHub.java @@ -6,8 +6,12 @@ import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; +import java.io.IOException; import java.util.*; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; + class SortByDate implements Comparator { public int compare(Album a1, Album a2) { return a1.getDate().compareTo(a2.getDate()); @@ -326,4 +330,13 @@ public class MusicHub { } xmlHandler.createXMLFile(document, ELEMENTS_FILE_PATH); } + + public void getSong(List Songs, String songTitle) throws NoAlbumFoundException, UnsupportedAudioFileException, IOException, LineUnavailableException { + for (AudioElement s : Songs) { + if (s.getTitle().equalsIgnoreCase(songTitle)) { + s.playSong(); + } + } + + } } \ No newline at end of file diff --git a/src/musichub/business/Song.java b/src/musichub/business/Song.java index 3e9011b..7b90156 100644 --- a/src/musichub/business/Song.java +++ b/src/musichub/business/Song.java @@ -1,5 +1,14 @@ package musichub.business; +import java.io.File; +import java.io.IOException; + +import javax.sound.sampled.AudioInputStream; +import javax.sound.sampled.AudioSystem; +import javax.sound.sampled.Clip; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; + import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -53,4 +62,6 @@ public class Song extends AudioElement { parentElement.appendChild(song); } + + } \ No newline at end of file diff --git a/src/musichub/main/Main.java b/src/musichub/main/Main.java index b72a95b..58080bd 100644 --- a/src/musichub/main/Main.java +++ b/src/musichub/main/Main.java @@ -2,11 +2,17 @@ package musichub.main; import musichub.business.*; +import java.io.IOException; +import java.util.ArrayList; import java.util.Iterator; +import java.util.List; import java.util.Scanner; +import javax.sound.sampled.LineUnavailableException; +import javax.sound.sampled.UnsupportedAudioFileException; + public class Main { - public static void main(String[] args) { + public static void main(String[] args) throws UnsupportedAudioFileException, IOException, LineUnavailableException { MusicHub theHub = new MusicHub(); @@ -55,7 +61,10 @@ public class Main { albumTitle = scan.nextLine(); try { + List songs = theHub.getAlbumSongs(albumTitle); System.out.println(theHub.getAlbumSongs(albumTitle)); + String song = scan.nextLine(); + theHub.getSong(songs, song); } catch (NoAlbumFoundException ex) { System.out.println("No album found with the requested title " + ex.getMessage()); } -- cgit v1.2.3