0

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 !

Tofuw
  • 908
  • 5
  • 16
  • 35
  • I would send audio over UDP instead of TCP. – bblincoe Jan 23 '14 at 13:53
  • @bblincoe Thanks for your answer. Even so, can I let the TCP for the moment or it will crash my app ? Why use UDP instead of TCP is better ? – Tofuw Jan 23 '14 at 13:58
  • 1
    UDP is really better suited for streaming media content. TCP offers quality-of-service (QoS) which guarantees that your message gets from one system to the other. In the case of audio (or video) you wouldn't notice any loss and would much rather prefer low latency (faster buffering). – bblincoe Jan 23 '14 at 14:08
  • I also believe that Android offers a library to stream audio with little knowledge on your part. Take a look at the Android API reference. – bblincoe Jan 23 '14 at 14:09
  • Thank you again for your specifications. For the moment, I'm focused on the Java Server part (PC). The Android part will be only used to "listen" the music, I'll see that later ^^ – Tofuw Jan 23 '14 at 14:17

0 Answers0