Visualizzazione dei risultati da 1 a 7 su 7

Discussione: Restart di un suono

  1. #1
    Utente di HTML.it L'avatar di andbaz
    Registrato dal
    Jul 2011
    Messaggi
    441

    Restart di un suono

    Con la pressione di un tasto della tastiera viene riprodotto un suono, ma se viene premuto di nuovo un tasto e il suono non è ancora terminato vorrei poterlo fermare e riprodurre nuovamente dall'inizio.

    Ho scritto questo codice ma non funziona, invece di fermarlo ne riproduce un altro sopra.

    Come devo fare?

    Grazie

    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 void main(String[] args) {         
             x frame = new x("x");
             frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
             frame.addComponentsToPane();          
             frame.pack();
             frame.setVisible(true);
             
        }
    
        private void addComponentsToPane() {                  
            typingArea = new JTextField();
            typingArea.addKeyListener(this);                  
            getContentPane().add(typingArea, BorderLayout.PAGE_START);
        }
         
        public x(String name) {
            super(name);
        }     
         
        public void keyPressed(KeyEvent e) {
            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); 
                Clip 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();
            }
        }
    
        /* ...codice...*/
    
        @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
    Invece di utilizzare una variabile locale crea una proprietà
    codice:
    private Clip audioClip;

    E all'inizio di keyPressed metti
    codice:
    if(audioClip != null) audioClip.stop();

  3. #3
    Utente di HTML.it L'avatar di andbaz
    Registrato dal
    Jul 2011
    Messaggi
    441
    Niente non funziona: ho fatto esattamente come mi hai scritto, ma succede la stessa cosa di prima.
    Secondo il codice, con update(), mi stampa n START consecutivi se io premo n volte un tasto della tastiera e il suono non è ancora terminato. Sicuro che così dovrebbe funzionare?

  4. #4
    Utente di HTML.it
    Registrato dal
    May 2012
    Messaggi
    1,453
    Quote Originariamente inviata da andbaz Visualizza il messaggio
    Niente non funziona: ho fatto esattamente come mi hai scritto, ma succede la stessa cosa di prima.
    Secondo il codice, con update(), mi stampa n START consecutivi se io premo n volte un tasto della tastiera e il suono non è ancora terminato. Sicuro che così dovrebbe funzionare?
    Nello stesso if chiama il metodo update come update(null) aggiungendo una condizione all'interno del if type == LineEvent.Type.STOP || type == null, poi posta il codice

  5. #5
    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");
            }
        }
    }

  6. #6
    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");
        }

  7. #7
    Utente di HTML.it L'avatar di andbaz
    Registrato dal
    Jul 2011
    Messaggi
    441

    [RISOLTO] Restart di un suono con clip al keypressed

    RISOLTO!
    Grazie mille, è 3 giorni che ci sbatto la testa.
    Sicuramente tornerà utile anche per altri!
    Ciao, buona giornata!
    Ultima modifica di andbaz; 02-12-2014 a 17:30

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 © 2025 vBulletin Solutions, Inc. All rights reserved.