IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

AWT/Swing Java Discussion :

Problem de socket vocal avec swing


Sujet :

AWT/Swing Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre actif
    Homme Profil pro
    Étudiant
    Inscrit en
    Septembre 2010
    Messages
    67
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 34
    Localisation : Canada

    Informations professionnelles :
    Activité : Étudiant
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Septembre 2010
    Messages : 67
    Par défaut Problem de socket vocal avec swing
    Bonjour donc jèssaye de faire une trasmission de son via les socket en java

    jài cree une interface swing afin de faire un bouton qui debut la trasmission du son en socket

    Lorsque je click sur le bouton ma swing `bugg" et se gele et ne veut plus se fermer...quelqu'un peut m'aider a corriger mon petit code?
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    package com.bass.chat;
     
     
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.ByteArrayOutputStream;
    import java.io.IOException;
    import java.net.ServerSocket;
    import java.net.Socket;
     
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.Mixer;
    import javax.sound.sampled.SourceDataLine;
    import javax.sound.sampled.TargetDataLine;
    import javax.swing.*;
     
     
     
     
     
    public class CallGUI extends JFrame {
     
    	private static final long serialVersionUID = 1L;
    	// will first hold "Username:", later on "Enter message"
    	public JLabel label;
    	// to hold the Username and later on the messages
    	private JTextField tf;
    	// to hold the server address an the port number
    	private JTextField tfServer, tfPort;
    	// to Logout and get the list of the users
    	private JButton login, logout, whoIsIn;
    	// for the chat room
    	private JTextArea ta;
    	// if it is for connection
    	private boolean connected;
    	public JFrame frame; 
    	// the Client object
    	private sender client;
    	// the default port number
    	private int defaultPort;
    	private String defaultHost;
    	ServerSocket MyService;
    	Socket clientSocket = null;
    	BufferedInputStream input;
    	TargetDataLine targetDataLine;
     
    	BufferedOutputStream out;
    	  ByteArrayOutputStream byteArrayOutputStream;
    	  AudioFormat audioFormat;	
     
    	  SourceDataLine sourceDataLine;	  
    	 byte tempBuffer[] = new byte[10000];
     public CallGUI(){
     
    	 initUI();
     
     
       }
     
     public final void initUI() {
    	   frame = new JFrame(); 
    	frame.getContentPane(); 
         JPanel panel = new JPanel();
       //  getContentPane().add(panel);
     	JPanel northPanel = new JPanel();
    	// the server name anmd the port number
    	JPanel serverAndPort = new JPanel();
     
     
         panel.setToolTipText("A Panel container");
         JLabel label = new JLabel();
         JButton button = new JButton(new ImageIcon("img\\Phone-icon.png"));
         button.setSize(100, 100);
         button.addActionListener(new BoutonListener());
         JButton button2 = new JButton("allo");
         northPanel.add(button2);
     
         button.setToolTipText("Make a call");
     
         panel.add(button);
         panel.add(label);
     
     
      //  frame.setLayout(new GridLayout(1, 2));
     
         frame.add(panel,BorderLayout.SOUTH);
           frame.add(northPanel);
     
     
     
         frame.setTitle("Tooltip");
         frame.setSize(600, 600);
         frame.setVisible(true);
         panel.setBackground(Color.DARK_GRAY);
     
         frame.setLocationRelativeTo(null);
         frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
     
     }
     class BoutonListener implements ActionListener { 
    	    //Red�finition de la m�thode actionPerformed()
    	    public void actionPerformed(ActionEvent arg0)   {
    	    	 try {
    				sender();
     
    			} catch (LineUnavailableException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
     
    	    }
    	  }
     void sender() throws LineUnavailableException{   
     	try {
     		audioFormat = getAudioFormat();
     		DataLine.Info dataLineInfo =  new DataLine.Info( SourceDataLine.class,audioFormat);
     		sourceDataLine = (SourceDataLine)
     	    AudioSystem.getLine(dataLineInfo);
     	    sourceDataLine.open(audioFormat);
     	    sourceDataLine.start();
    			MyService = new ServerSocket(1500);
    			clientSocket = MyService.accept();
    			captureAudio();
    			input = new BufferedInputStream(clientSocket.getInputStream());	
    			out=new BufferedOutputStream(clientSocket.getOutputStream());
     
    			while(input.read(tempBuffer)!=-1){			
    				sourceDataLine.write(tempBuffer,0,10000);
    			}
    		} catch (IOException e) {
     
    			e.printStackTrace();
    		}
     
    	}
    	 private AudioFormat getAudioFormat(){
    		    float sampleRate = 8000.0F;		  
    		    int sampleSizeInBits = 16;		   
    		    int channels = 1;		    
    		    boolean signed = true;		    
    		    boolean bigEndian = false;		 
    		    return new AudioFormat(
    		                      sampleRate,
    		                      sampleSizeInBits,
    		                      channels,
    		                      signed,
    		                      bigEndian);
    		  }
     
     
     
    	private void captureAudio() {
    		try {
     
    			Mixer.Info[] mixerInfo = AudioSystem.getMixerInfo();
    			System.out.println("Available mixers:");
    			for (int cnt = 0; cnt < mixerInfo.length; cnt++) {
    				System.out.println(mixerInfo[cnt].getName());
    			}
    			audioFormat = getAudioFormat();
     
    			DataLine.Info dataLineInfo = new DataLine.Info(
    					TargetDataLine.class, audioFormat);
     
    			Mixer mixer = AudioSystem.getMixer(mixerInfo[3]);
     
    			targetDataLine = (TargetDataLine) mixer.getLine(dataLineInfo);
     
    			targetDataLine.open(audioFormat);
    			targetDataLine.start();
     
    			Thread captureThread = new CaptureThread();
    			captureThread.start();		
    		} catch (Exception e) {
    			System.out.println(e);
    			System.exit(0);
    		}
    	}
     
    	class CaptureThread extends Thread {
     
    		byte tempBuffer[] = new byte[10000];
     
    		public void run() {			
    			try {
    				while (true) {
    					int cnt = targetDataLine.read(tempBuffer, 0,
    							tempBuffer.length);
    					out.write(tempBuffer);				
    				}
     
    			} catch (Exception e) {
    				System.out.println(e);
    				System.exit(0);
    			}
    		}
    	}
    }

  2. #2
    Expert éminent
    Avatar de tchize_
    Homme Profil pro
    Ingénieur développement logiciels
    Inscrit en
    Avril 2007
    Messages
    25 482
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 46
    Localisation : Belgique

    Informations professionnelles :
    Activité : Ingénieur développement logiciels
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2007
    Messages : 25 482

Discussions similaires

  1. Probleme rafraichissement animation avec Swing
    Par Philinux dans le forum AWT/Swing
    Réponses: 1
    Dernier message: 01/07/2010, 02h53
  2. Probleme avec Swing GUI Builder
    Par kucai dans le forum NetBeans Platform
    Réponses: 1
    Dernier message: 30/06/2010, 12h27
  3. Probleme d'utilisation des sockets avec Swing
    Par tuxman dans le forum AWT/Swing
    Réponses: 5
    Dernier message: 09/11/2008, 11h57
  4. Probleme Serveur/Multi-client avec socket
    Par CouaC dans le forum Réseau
    Réponses: 9
    Dernier message: 18/06/2007, 00h49
  5. App Socket Raw avec VC++6
    Par Martin Soucy dans le forum Développement
    Réponses: 3
    Dernier message: 04/12/2002, 05h07

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo