0

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.

Tim
  • 5
  • 5
  • 2
    Please post the stacktrace. We're not psychic. – nanofarad Oct 17 '13 at 23:06
  • 17 blank lines in a row... impressive. – chrylis -cautiouslyoptimistic- Oct 17 '13 at 23:08
  • @hexafraction sorry about that ive posted it – Tim Oct 17 '13 at 23:42
  • Which line is line 73? Or do you want us to count out 73 lines? – nanofarad Oct 17 '13 at 23:43
  • @hexafraction THe 73rd line is the start of the if else statement in receiving messages – Tim Oct 17 '13 at 23:49
  • Is there a way on stopping the songs when it receives the stop message or when the user sends a new message like from rock1 he sends rock2 so the first song will automatically stop then play the next – Tim Oct 18 '13 at 00:15
  • 1
    Do you mean this line? " if (message.equals("rock1")) {" if so, your message variable is null, and the problem may be with reading messages not stopping the clips. But if you can get all three clips playing, that does not follow, so I don't know. By the way, the bracketing is confusing to me. I'm reluctant to try very hard to diagnose when I have to count brackets. – Phil Freihofner Oct 18 '13 at 22:04

1 Answers1

0

The comment above by Phil is right.

It's usually safer to write "stop".equals(message) than message.equals("stop"), if you want to avoid NullPointerExceptions.

Also, you never seem to check the validity of message. readLine() returns null, if there's no more input, i.e. the other side just hung up. In that case you might want to wait for the next request.

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

Cheers.

Hendrik
  • 5,085
  • 24
  • 56