Add Song class for storing details about queue items.
This commit is contained in:
parent
ed610ebdae
commit
367f2b93de
|
@ -9,6 +9,8 @@ import java.io.Closeable;
|
|||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.URL;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.*;
|
||||
|
@ -150,11 +152,18 @@ public class Main extends ListenerAdapter
|
|||
{
|
||||
try
|
||||
{
|
||||
Song song = new Song(new URL(arg));
|
||||
event.getChannel().sendMessage("Downloading ...").queue();
|
||||
String destination = downloadSong(arg);
|
||||
musicHandler.addSong(new File(destination));
|
||||
boolean ok = downloadSong(song);
|
||||
if (ok)
|
||||
{
|
||||
musicHandler.addSong(song);
|
||||
event.getChannel().sendMessage("Downloaded and added to queue!").queue();
|
||||
}
|
||||
|
||||
} catch (MalformedURLException ex)
|
||||
{
|
||||
event.getChannel().sendMessage("That's not a valid URL you idiot! " + ex.getMessage()).queue();
|
||||
} catch (IOException | InterruptedException | RuntimeException ex)
|
||||
{
|
||||
event.getChannel().sendMessage("Failed to download! Reason: " + ex.getMessage()).queue();
|
||||
|
@ -162,9 +171,9 @@ public class Main extends ListenerAdapter
|
|||
}
|
||||
}
|
||||
|
||||
private String downloadSong(String arg) throws IOException, RuntimeException, InterruptedException
|
||||
private boolean downloadSong(Song song) throws IOException, RuntimeException, InterruptedException
|
||||
{
|
||||
String cmd = "/usr/bin/youtube-dl -x --audio-format=wav " + arg;
|
||||
String cmd = "/usr/bin/youtube-dl -x --audio-format=wav " + song.getUrl().toString();
|
||||
System.out.println("Running command: " + cmd);
|
||||
// Process exec = Runtime.getRuntime().exec().split(" "));
|
||||
Process exec = new ProcessBuilder(cmd.split(" ")).redirectOutput(ProcessBuilder.Redirect.PIPE).start();
|
||||
|
@ -176,8 +185,10 @@ public class Main extends ListenerAdapter
|
|||
throw new RuntimeException("youtube-dl failed with error " + exec.exitValue());
|
||||
Matcher matcher = Pattern.compile("Destination: (.*\\.wav)").matcher(output);
|
||||
matcher.find();
|
||||
String destination = matcher.group(1);
|
||||
return destination;
|
||||
song.setLocation(new File(matcher.group(0)));
|
||||
return true;
|
||||
// String destination = matcher.group(1);
|
||||
// return destination;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -26,8 +26,8 @@ import net.dv8tion.jda.api.audio.AudioSendHandler;
|
|||
public class MusicHandler implements AudioSendHandler, Closeable
|
||||
{
|
||||
|
||||
private final Queue<File> songQueue = new ConcurrentLinkedQueue<>();
|
||||
private File currentSong;
|
||||
private final Queue<Song> songQueue = new ConcurrentLinkedQueue<>();
|
||||
private Song currentSong;
|
||||
private AudioInputStream din = null;
|
||||
private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
|
||||
private int byteCount;
|
||||
|
@ -36,10 +36,10 @@ public class MusicHandler implements AudioSendHandler, Closeable
|
|||
{
|
||||
}
|
||||
|
||||
public void addSong(File file)
|
||||
public void addSong(Song song)
|
||||
{
|
||||
System.out.println("Song added to queue: " + file.getAbsolutePath());
|
||||
songQueue.add(file);
|
||||
System.out.println("Song added to queue: " + song.getLocation().getAbsolutePath());
|
||||
songQueue.add(song);
|
||||
if (!canProvide())
|
||||
nextSong();
|
||||
}
|
||||
|
@ -62,8 +62,8 @@ public class MusicHandler implements AudioSendHandler, Closeable
|
|||
currentSong = songQueue.poll();
|
||||
if (currentSong == null)
|
||||
return false;
|
||||
System.out.println("Playing song " + currentSong.getAbsolutePath());
|
||||
in = AudioSystem.getAudioInputStream(currentSong);
|
||||
System.out.println("Playing song " + currentSong.getLocation().getAbsolutePath());
|
||||
in = AudioSystem.getAudioInputStream(currentSong.getLocation());
|
||||
AudioFormat decodedFormat = AudioSendHandler.INPUT_FORMAT;
|
||||
din = AudioSystem.getAudioInputStream(decodedFormat, in);
|
||||
byteCount = 3840;
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package moe.nekojimi.chords;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author jimj316
|
||||
*/
|
||||
public class Song
|
||||
{
|
||||
private String title;
|
||||
private String artist;
|
||||
private final URL url;
|
||||
private File location = null;
|
||||
|
||||
public Song(URL url)
|
||||
{
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public String getTitle()
|
||||
{
|
||||
return title;
|
||||
}
|
||||
|
||||
public void setTitle(String title)
|
||||
{
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getArtist()
|
||||
{
|
||||
return artist;
|
||||
}
|
||||
|
||||
public void setArtist(String artist)
|
||||
{
|
||||
this.artist = artist;
|
||||
}
|
||||
|
||||
public URL getUrl()
|
||||
{
|
||||
return url;
|
||||
}
|
||||
|
||||
public File getLocation()
|
||||
{
|
||||
return location;
|
||||
}
|
||||
|
||||
public void setLocation(File location)
|
||||
{
|
||||
this.location = location;
|
||||
}
|
||||
|
||||
void delete()
|
||||
{
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue