gpt4 book ai didi

java - 我需要有关为何为什么要获得NPE以及如何在播放时停止剪辑的帮助

转载 作者:行者123 更新时间:2023-12-03 02:16:31 26 4
gpt4 key购买 nike

您好,我急切地想寻找一种解决方案,在它已经运行时如何停止我的剪辑。我一直在寻找解决方案已有多少天了,但仍然找不到。请任何知道如何帮助我的人。

这是我的代码

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");
}


}
}

}

问题是当收到消息“停止”时歌曲没有停止
并且当它收到另一个消息,例如“rock2”时,即使已经有歌曲在播放,它也将继续播放。请帮助我修复此程序。

这是我的java stacktrace控制台:
 Exception in thread "main" java.lang.NullPointerException
at server.main(server.java:73)

它可以同时播放所有3首歌曲,但无法停止播放,当我尝试发送2条相同的消息时,服务器崩溃并显示上述错误。

最佳答案

菲尔(Phil)的上述评论是正确的。

如果要避免"stop".equals(message),通常写message.equals("stop")NullPointerExceptions更安全。

另外,您似乎从未检查过message的有效性。如果没有更多输入,即另一侧刚刚挂断,readLine()返回null。在这种情况下,您可能要等待下一个请求。

[...]
message = bufferedReader.readLine();
// the following line may solve the issue:
if (message == null) continue;

干杯。

关于java - 我需要有关为何为什么要获得NPE以及如何在播放时停止剪辑的帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19438815/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com