Visualizzazione dei risultati da 1 a 7 su 7

Discussione: Restart di un suono

Hybrid View

  1. #1
    Utente di HTML.it L'avatar di andbaz
    Registrato dal
    Jul 2011
    Messaggi
    441
    Allora ho provato a fare come mi hai detto ma non riesco a far passare la variabile type in keyPressed...purtroppo con java ho ancora poca confidenza

    In ogni caso ho provato a fare anche così ma non funziona, le ho provate tutte e l'ultima è questa ma non riesco a passare audioClip in keyPressed

    codice:
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import javax.sound.sampled.*;
     
    public class x extends JFrame
            implements KeyListener,
            ActionListener,
            LineListener
    {
    
    
        JTextField typingArea;
        static String audioFilePath;
        boolean playCompleted;
    	public static Clip audioClip;
         
        public static void main(String[] args) {
        	
             x frame = new x("x");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.addComponentsToPane();          
             frame.pack();
             frame.setVisible(true);
             
             try {
    	         String audioFilePath = "/C:/Users/user/Desktop/ECLIPSE/1/bin/65.wav";
    	         File audioFile = new File(audioFilePath);
    	         AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); 
    	         AudioFormat format = audioStream.getFormat(); 
    	         DataLine.Info info = new DataLine.Info(Clip.class, format); 
    	         Clip audioClip = (Clip) AudioSystem.getLine(info);
    	         audioClip.open(audioStream);
             } catch (UnsupportedAudioFileException ex) {
                 ex.printStackTrace();
             } catch (LineUnavailableException ex) {
                ex.printStackTrace();
             } catch (IOException ex) {
                ex.printStackTrace();
         	}       
        }
        
        public void keyPressed(KeyEvent e) {
            audioClip.stop();
            ((Clip) audioClip).setFramePosition(0);
            audioClip.start();
            audioClip.addLineListener((LineListener) this);
        }
        
        private void addComponentsToPane() {                  
            typingArea = new JTextField();
            typingArea.addKeyListener(this);                  
            getContentPane().add(typingArea, BorderLayout.PAGE_START);
        }
         
        public x(String name) {
            super(name);
        }
    
    
    	@Override
        public void update(LineEvent event) {
            LineEvent.Type type = event.getType();        
            if (type == LineEvent.Type.START) {
                playCompleted = false;
                System.out.println("START");
                 
            } else if (type == LineEvent.Type.STOP) {
                playCompleted = true;
                System.out.println("STOP");
            }
        }
    }

  2. #2
    Utente di HTML.it
    Registrato dal
    May 2012
    Messaggi
    1,453
    Quote Originariamente inviata da andbaz Visualizza il messaggio
    Allora ho provato a fare come mi hai detto ma non riesco a far passare la variabile type in keyPressed...purtroppo con java ho ancora poca confidenza

    In ogni caso ho provato a fare anche così ma non funziona, le ho provate tutte e l'ultima è questa ma non riesco a passare audioClip in keyPressed

    codice:
    import java.awt.BorderLayout;
    import java.awt.event.*;
    import javax.swing.*;
    import java.io.*;
    import javax.sound.sampled.*;
     
    public class x extends JFrame
            implements KeyListener,
            ActionListener,
            LineListener
    {
    
    
        JTextField typingArea;
        static String audioFilePath;
        boolean playCompleted;
        public static Clip audioClip;
         
        public static void main(String[] args) {
            
             x frame = new x("x");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.addComponentsToPane();          
             frame.pack();
             frame.setVisible(true);
             
             try {
                 String audioFilePath = "/C:/Users/user/Desktop/ECLIPSE/1/bin/65.wav";
                 File audioFile = new File(audioFilePath);
                 AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile); 
                 AudioFormat format = audioStream.getFormat(); 
                 DataLine.Info info = new DataLine.Info(Clip.class, format); 
                 Clip audioClip = (Clip) AudioSystem.getLine(info);
                 audioClip.open(audioStream);
             } catch (UnsupportedAudioFileException ex) {
                 ex.printStackTrace();
             } catch (LineUnavailableException ex) {
                ex.printStackTrace();
             } catch (IOException ex) {
                ex.printStackTrace();
             }       
        }
        
        public void keyPressed(KeyEvent e) {
            audioClip.stop();
            ((Clip) audioClip).setFramePosition(0);
            audioClip.start();
            audioClip.addLineListener((LineListener) this);
        }
        
        private void addComponentsToPane() {                  
            typingArea = new JTextField();
            typingArea.addKeyListener(this);                  
            getContentPane().add(typingArea, BorderLayout.PAGE_START);
        }
         
        public x(String name) {
            super(name);
        }
    
    
        @Override
        public void update(LineEvent event) {
            LineEvent.Type type = event.getType();        
            if (type == LineEvent.Type.START) {
                playCompleted = false;
                System.out.println("START");
                 
            } else if (type == LineEvent.Type.STOP) {
                playCompleted = true;
                System.out.println("STOP");
            }
        }
    }
    Fai cosi

    codice:
        private Clip audioClip;
    
        public void keyPressed(KeyEvent e) {
            if(audioClip != null) audioClip.stop();
            String audioFilePath = "/C:/Users/user/Desktop/ECLIPSE/1/bin/65.wav";
            File audioFile = new File(audioFilePath);
            try {
                AudioInputStream audioStream = AudioSystem.getAudioInputStream(audioFile);
                AudioFormat format = audioStream.getFormat();
                DataLine.Info info = new DataLine.Info(Clip.class, format);
                audioClip = (Clip) AudioSystem.getLine(info);
                audioClip.addLineListener((LineListener) this);
                audioClip.open(audioStream);
                audioClip.setFramePosition(0);
                audioClip.start();
            } catch (UnsupportedAudioFileException ex) {
                ex.printStackTrace();
            } catch (LineUnavailableException ex) {
                ex.printStackTrace();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    
        @Override
        public void update(LineEvent event) {
            LineEvent.Type type = event.getType();
    
            if (type == LineEvent.Type.START) start();
            else if (type == LineEvent.Type.STOP) stop();
        }
    
        private void start(){
            playCompleted = false;
            System.out.println("START");
        }
    
        private void stop(){
            playCompleted = true;
            System.out.println("STOP");
        }

Tag per questa discussione

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.