Hi Im desperately looking for a solution on how will I stop my clip when it is already running I've been looking for solutions for how many days now but still I cant find one. Please anyone who knows how help me.
Here is my code
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javazoom.jl.player.Player;
public class server
{
private static final int PORT = 7777;
private static ServerSocket serverSocket;
private static Socket clientSocket;
private static InputStreamReader inputStreamReader;
private static BufferedReader bufferedReader;
private static String message;
@SuppressWarnings("unused")
private String filename;
private static Clip clip1;
private static Clip clip2;
private static Clip clip3;
private static Clip clip4;
private static Clip clip5;
private static Clip clip6;
static Player player;
public server(String filename) {
this.filename = filename;
}
public static void main(String[] args) throws Exception
{
try
{
serverSocket = new ServerSocket(PORT, 0, InetAddress.getLocalHost());
System.out.println("IP: " + serverSocket.getInetAddress() + " Port: " + serverSocket.getLocalPort());
} catch (IOException e)
{
System.out.println("Could not listen on port: 7777");
}
System.out.println("Server started. Listening to the port 7777");
while (true)
{
try
{
clientSocket = serverSocket.accept();
inputStreamReader = new InputStreamReader(clientSocket.getInputStream());
bufferedReader = new BufferedReader(inputStreamReader);
message = bufferedReader.readLine();
System.out.println(message);{
clip1 = null;
clip2 = null;
clip3 = null;
clip4 = null;
clip5 = null;
clip6 = null;
if (message.equals("rock1")) {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("D:/Andrd/Music/TheAll-AmericanRejects-Swing,Swing.wav"));
clip4 = AudioSystem.getClip();
clip4.open(inputStream);
clip4.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "rock1 song is playing!");
}
});
} else if (message.equals("stop") && clip4 != null && clip4.isRunning()){
clip4.stop();
}
if(message.equals("rock2")) {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("D:/Andrd/Music/FallOutBoy-SugarWe'reGoingDownWithLyrics!HQ.wav"));
clip5 = AudioSystem.getClip();
clip5.open(inputStream);
clip5.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "rock2 song is playing!");
}
});
} else if(message.equals("stop") && clip5 != null && clip5.isRunning()) {
clip5.stop();
}
if(message.equals("rock3")) {
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("D:/Andrd/Music/Fall Out Boy Light Em Up Lyrics.wav"));
clip6 = AudioSystem.getClip();
clip6.open(inputStream);
clip6.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "rock3 song is playing!");
}
});
} else if(message.equals("stop") && clip6 !=null && clip6.isRunning()){
clip6.stop();
}
if(message.equals("punk1")){
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("D:/Andrd/Music/athousandmiles.wav"));
clip1 = AudioSystem.getClip();
clip1.open(inputStream);
clip1.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "punk1 song is playing!");
}
});
} else if(message.equals("stop") && clip1 !=null && clip1.isRunning()){
clip1.stop();
}
if(message.equals("punk2")){
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("D:/Andrd/Music/ISSUES - Boyfriend Lyric Video (Punk Goes Pop 5).wav"));
clip2 = AudioSystem.getClip();
clip2.open(inputStream);
clip2.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "punk2 song is playing!");
}
});
} else if(message.equals("stop") && clip2 !=null && clip2.isRunning()){
clip2.stop();
}
if(message.equals("punk3")){
AudioInputStream inputStream = AudioSystem.getAudioInputStream(new File("D:/Andrd/Music/My Chemical Romance - Famous Last Words (Lyrics) - GetThemLyrics.wav"));
clip2 = AudioSystem.getClip();
clip2.open(inputStream);
clip2.start();
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JOptionPane.showMessageDialog(null, "punk3 song is playing!");
}
});
} else if(message.equals("stop") && clip3 !=null && clip3.isRunning()){
clip3.stop();
}
}
inputStreamReader.close();
clientSocket.close();
}catch (IOException ex)
{
System.out.println("Problem in message reading");
}
}
}
}
The Problem is the song is not stopping when it receives the message "stop" and when it receives another message like "rock2" it will just play it even if there is a song that is already playing. PLEASE HELP me fix this program..
This is my java stacktrace console:
Exception in thread "main" java.lang.NullPointerException
at server.main(server.java:73)
It can play all 3 songs at the same time but there is no stopping it and when i try to send 2 same message the server crashes and show the above error.