Question

I have a Java server and an audio file. With my class, below.

I can playback my audio file. Now, I would like to broadcast it through the TCP/IP. I would like to send this audio flow to my Client (which is an Android device).

My Sample Code

public class ServerBoard extends JFrame implements Observer
{
    private TCPServer tcpserver;

    JToolBar toolbarsouth;
    private Thread audioPlayerThread;
    private Sound player;
    private JButton loadmusic;
    private JButton playmusic;
    private JButton stopmusic;
    private JLabel title;
    private File audioFile;

    /**
     * 
     * @param l
     * @param h
     */
    public ServerBoard(int l, int h)
    {
        super("ServerBoard");
        this.initialize();
        this.setSize(l,h);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setVisible(true);
    }

    // INITIALIZATION OF THE SERVER WINDOW
    public void initialize()
    {
        Container c=this.getContentPane();
        c.add(this.createSouth(),BorderLayout.SOUTH);   
    }

    // CREATION OF THE SOUTH PANEL
    public JPanel createSouth()
    {
        JPanel panelsouth=new JPanel();
        toolbarsouth=new JToolBar();

        this.loadmusic=new JButton("LOAD MUSIC");
        loadmusic.addActionListener(new ServerBoardListener());
        toolbarsouth.add(loadmusic);

        this.playmusic=new JButton("PLAY MUSIC");
        playmusic.addActionListener(new ServerBoardListener());
        toolbarsouth.add(playmusic);

        this.stopmusic=new JButton("STOP MUSIC");
        stopmusic.addActionListener(new ServerBoardListener());
        toolbarsouth.add(stopmusic);

        this.title=new JLabel("No music currently playing");

        panelsouth.add(toolbarsouth);
        panelsouth.add(title);
        return panelsouth;  
    }   

    private class ServerBoardListener implements ActionListener
    {    
        public void actionPerformed(ActionEvent e)
        {
            String namebutton=e.getActionCommand().trim();

            if(namebutton.equals("START SERVER"))
                startServer();

            else if(namebutton.equals("LOAD MUSIC"))
                loadMusic();

            else if(namebutton.equals("PLAY MUSIC"))
                playMusic();

            else if(namebutton.equals("STOP MUSIC"))
                stopMusic();
        }
    }

/***************************************************************************
 *********************************** METHODS *******************************
 ***************************************************************************/
    // START THE SERVER - Waiting for a client connexion
    public void startServer()
    {
        // When the button was clicked, set the button disable
        startserver.setEnabled(false);

        // Creation of OnMessageReceived object, called by TCPServer
        tcpserver=new TCPServer(new TCPServer.OnMessageReceived()
        {
            /*
             * This method was declared in TCPServer like an interface
             * but is implemented here
             * It is a CALLBACK : this method will run each time TCPServer will called it
             */
            public void messageReceived(String message)
            {
                messagearea.append(message);
            }
        });
        tcpserver.start();
    }

    // LOAD MUSIC
    public void loadMusic()
    {
        JFileChooser chooser=new JFileChooser();

        int res=chooser.showOpenDialog(toolbarsouth);
        if (res== JFileChooser.APPROVE_OPTION) 
        {
            audioFile = chooser.getSelectedFile();
            player=new Sound();

            title.setText("Now playing : "+audioFile.getName());
            player.setFile(audioFile);
            player.init();          
        }
    }

    // PLAY MUSIC - First, load a WAV audio file
    public void playMusic()
    {
        player.init();
        audioPlayerThread = new Thread(player);
        audioPlayerThread.start();

        player.getLine().addLineListener(new LineListener() 
        {
            public void update(LineEvent lineevent) 
            {
                if (lineevent.getType() == LineEvent.Type.STOP)
                {
                    playmusic.setEnabled(true);
                    stopmusic.setEnabled(false);
                    System.out.println("stop");
                }
                if (lineevent.getType() == LineEvent.Type.START) 
                {
                    playmusic.setEnabled(false);
                    stopmusic.setEnabled(true);
                    System.out.println("Reading");
                }
            }
        });
    }

    // STOP THE MUSIC
    public void stopMusic()
    {
        // Thread.currentThread().interrupt();
        player.stop();
    }           
}

Can you help me please ?

Thanks in advance for your answers !

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top