こんにちは、クリップが既に実行されているときにクリップを停止する方法についての解決策を必死に探しています。今何日も解決策を探していますが、まだ見つかりません。方法を知っている人は私を助けてください。
これが私のコードです
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");
}
}
}
}
問題は、「stop」というメッセージを受信しても曲は停止せず、「rock2」などの別のメッセージを受信すると、すでに再生中の曲がある場合でも再生することです。このプログラムを修正するのを手伝ってください..
これは私のJavaスタックトレースコンソールです:
Exception in thread "main" java.lang.NullPointerException
at server.main(server.java:73)
3曲すべてを同時に再生できますが、停止することはできません.2つの同じメッセージを送信しようとすると、サーバーがクラッシュし、上記のエラーが表示されます.