2013-10-17 36 views
0

嗨我拼命尋找一個解決方案,當它已經運行時我將如何停止我的剪輯我一直在尋找解決方案現在很多天,但我仍然找不到一個。請任何人知道如何幫助我。我需要幫助,爲什麼我會得到NPE以及如何在播放時停止剪輯

這裏是我的代碼

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堆棧跟蹤控制檯:

Exception in thread "main" java.lang.NullPointerException 
    at server.main(server.java:73) 

它可以在同一時間播放所有3首歌曲,但沒有阻止它,當我嘗試發送2相同消息服務器崩潰並顯示上述錯誤。

+2

請發佈堆棧跟蹤。我們不是心靈的。 – hexafraction

+0

連續17行空白......令人印象深刻。 – chrylis

+0

@hexafraction對不起,我發佈了它 – Tim

回答

0

菲爾上面的評論是正確的。

如果您想避開NullPointerExceptions,那麼編寫"stop".equals(message)message.equals("stop")通常更安全。

此外,你似乎從來沒有檢查message的有效性。 readLine()返回null,如果沒有更多的輸入,即另一方剛掛斷。在這種情況下,您可能需要等待下一個請求。

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

乾杯。

相關問題