Added functionality for music playback and basic library view.
This commit is contained in:
parent
dbe518faaa
commit
a1515ee9ad
5
pom.xml
5
pom.xml
@ -25,6 +25,11 @@
|
||||
<artifactId>hsqldb</artifactId>
|
||||
<version>2.3.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.googlecode.gstreamer-java</groupId>
|
||||
<artifactId>gstreamer-java</artifactId>
|
||||
<version>1.5</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
|
@ -3,6 +3,7 @@ package musicplayer;
|
||||
import musicplayer.db.DatabaseManager;
|
||||
import musicplayer.db.Gateway;
|
||||
import musicplayer.model.ExtractedMetadata;
|
||||
import musicplayer.model.Song;
|
||||
import org.jaudiotagger.audio.AudioFileIO;
|
||||
import org.jaudiotagger.audio.exceptions.CannotReadException;
|
||||
import org.jaudiotagger.audio.exceptions.InvalidAudioFrameException;
|
||||
@ -14,19 +15,45 @@ import java.io.*;
|
||||
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
|
||||
public class Application {
|
||||
|
||||
String musicFileExtensionRegex = ".*\\.(mp3|mp4|flac)";
|
||||
|
||||
public static void main(String[] args) {
|
||||
new Application();
|
||||
}
|
||||
final String musicFileExtensionRegex = ".*\\.(mp3|mp4|flac)";
|
||||
|
||||
public Application(){
|
||||
DatabaseManager.init();
|
||||
List<Song> songs = Gateway.listAllSongs().get();
|
||||
boolean running = true;
|
||||
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
|
||||
Player player = new Player();
|
||||
while(running){
|
||||
try {
|
||||
System.out.println("Ready to read");
|
||||
String input = br.readLine();
|
||||
System.out.println(input);
|
||||
switch (input) {
|
||||
case "play":
|
||||
player.playSong(songs.get(0).getSongFile());
|
||||
break;
|
||||
case "stop":
|
||||
player.stop();
|
||||
break;
|
||||
case "pause":
|
||||
player.pause();
|
||||
break;
|
||||
case "resume":
|
||||
player.resume();
|
||||
break;
|
||||
case "quit":
|
||||
running = false;
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
61
src/main/java/musicplayer/Player.java
Normal file
61
src/main/java/musicplayer/Player.java
Normal file
@ -0,0 +1,61 @@
|
||||
package musicplayer;
|
||||
|
||||
import org.gstreamer.ElementFactory;
|
||||
import org.gstreamer.Gst;
|
||||
import org.gstreamer.State;
|
||||
import org.gstreamer.elements.PlayBin2;
|
||||
|
||||
import java.io.File;
|
||||
|
||||
public class Player{
|
||||
|
||||
private PlayBin2 playBin;
|
||||
|
||||
private InternalThread internalThread;
|
||||
private Thread thread;
|
||||
|
||||
public Player(){
|
||||
Gst.init();
|
||||
playBin = new PlayBin2("BusMessages");
|
||||
playBin.setVideoSink(ElementFactory.make("fakesink", "videosink"));
|
||||
}
|
||||
|
||||
public void playSong(File songFile){
|
||||
if(playBin.getState() == State.PLAYING)
|
||||
stop();
|
||||
playBin.setURI(songFile.toURI());
|
||||
internalThread = new InternalThread();
|
||||
thread = new Thread(internalThread);
|
||||
playBin.play();
|
||||
thread.start();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
if (this.thread != null) {
|
||||
playBin.stop();
|
||||
internalThread.stop();
|
||||
thread.interrupt();
|
||||
thread = null;
|
||||
}
|
||||
}
|
||||
|
||||
public void resume(){
|
||||
playBin.play();
|
||||
}
|
||||
|
||||
public void pause(){
|
||||
playBin.pause();
|
||||
}
|
||||
|
||||
private class InternalThread implements Runnable {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
Gst.main();
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
Gst.quit();
|
||||
}
|
||||
}
|
||||
}
|
39
src/main/java/musicplayer/PlayerGUI.form
Normal file
39
src/main/java/musicplayer/PlayerGUI.form
Normal file
@ -0,0 +1,39 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="musicplayer.PlayerGUI">
|
||||
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||
<margin top="0" left="0" bottom="0" right="0"/>
|
||||
<constraints>
|
||||
<xy x="20" y="20" width="500" height="400"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<scrollpane id="2b209">
|
||||
<constraints>
|
||||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="6b9fd" class="javax.swing.JTree" binding="libraryView">
|
||||
<constraints/>
|
||||
<properties/>
|
||||
</component>
|
||||
</children>
|
||||
</scrollpane>
|
||||
<scrollpane id="359a1">
|
||||
<constraints>
|
||||
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
|
||||
</constraints>
|
||||
<properties/>
|
||||
<border type="none"/>
|
||||
<children>
|
||||
<component id="b8fb" class="javax.swing.JList" binding="list1" default-binding="true">
|
||||
<constraints/>
|
||||
<properties/>
|
||||
</component>
|
||||
</children>
|
||||
</scrollpane>
|
||||
</children>
|
||||
</grid>
|
||||
</form>
|
69
src/main/java/musicplayer/PlayerGUI.java
Normal file
69
src/main/java/musicplayer/PlayerGUI.java
Normal file
@ -0,0 +1,69 @@
|
||||
package musicplayer;
|
||||
|
||||
import musicplayer.db.DatabaseManager;
|
||||
import musicplayer.db.Gateway;
|
||||
import musicplayer.model.Album;
|
||||
import musicplayer.model.Song;
|
||||
|
||||
import javax.swing.*;
|
||||
import javax.swing.tree.DefaultMutableTreeNode;
|
||||
import javax.swing.tree.DefaultTreeModel;
|
||||
import javax.swing.tree.TreeNode;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class PlayerGUI {
|
||||
private JTree libraryView;
|
||||
private JList list1;
|
||||
private JPanel mainPanel;
|
||||
|
||||
|
||||
public static void main(String[] args) {
|
||||
JFrame frame = new JFrame();
|
||||
frame.setContentPane(new PlayerGUI().mainPanel);
|
||||
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
|
||||
frame.pack();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
|
||||
public PlayerGUI(){
|
||||
DatabaseManager.init();
|
||||
populateLibrary(Gateway.listAllSongsGroupedByAlbum().get());
|
||||
}
|
||||
|
||||
private void resetTree(){
|
||||
libraryView.removeAll();
|
||||
DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode();
|
||||
DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
|
||||
libraryView.setModel(treeModel);
|
||||
libraryView.setRootVisible(false);
|
||||
libraryView.setToggleClickCount(1);
|
||||
}
|
||||
|
||||
private void populateLibrary(Map<Album, List<Song>> libraryData){
|
||||
resetTree();
|
||||
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)libraryView.getModel().getRoot();
|
||||
libraryData.forEach((k, v) -> {
|
||||
DefaultMutableTreeNode albumNode = new DefaultMutableTreeNode(k);
|
||||
addNodeToTreeModel(parentNode, albumNode);
|
||||
v.forEach(x -> addNodeToTreeModel(albumNode, new DefaultMutableTreeNode(x)));
|
||||
});
|
||||
}
|
||||
|
||||
private void populateLibrary(List<Song> libraryData){
|
||||
resetTree();
|
||||
DefaultMutableTreeNode parentNode = (DefaultMutableTreeNode)libraryView.getModel().getRoot();
|
||||
libraryData.forEach(x -> addNodeToTreeModel(parentNode, new DefaultMutableTreeNode(x)));
|
||||
|
||||
}
|
||||
|
||||
private void addNodeToTreeModel(DefaultMutableTreeNode parentNode, DefaultMutableTreeNode node ) {
|
||||
DefaultTreeModel libraryModel = (DefaultTreeModel)libraryView.getModel();
|
||||
libraryModel.insertNodeInto(node, parentNode, parentNode.getChildCount());
|
||||
if (parentNode == libraryModel.getRoot()) {
|
||||
libraryModel.nodeStructureChanged((TreeNode)libraryModel.getRoot());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -19,7 +19,7 @@ public class Album {
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return String.format("ID: %d, Name: %s", id, name);
|
||||
return name;
|
||||
}
|
||||
|
||||
@SuppressWarnings("MethodWithMultipleReturnPoints")
|
||||
|
@ -26,7 +26,7 @@ public class Artist {
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return String.format("ID: %d, Name: %s", id, name);
|
||||
return name;
|
||||
}
|
||||
|
||||
@SuppressWarnings("MethodWithMultipleReturnPoints")
|
||||
|
@ -42,7 +42,7 @@ public class Song {
|
||||
|
||||
@Override
|
||||
public String toString(){
|
||||
return String.format("Artist: %s, Title: %s, Album: %s, Genre: %s", artist.getName(), title, album.getName(), genre);
|
||||
return title;
|
||||
}
|
||||
|
||||
@SuppressWarnings("MethodWithMultipleReturnPoints")
|
||||
|
Loading…
Reference in New Issue
Block a user