Visualizzazione dei risultati da 1 a 7 su 7
  1. #1
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305

    [JAVA] Riprodurre file wav

    Salve sto tentando di riprodurre un file audio in java, ma non ci riesco . sapreste aiutarmi?
    codice:
    File allarme = new File("becareful.wav");         
            try (AudioInputStream ais = AudioSystem.getAudioInputStream(allarme);
                    Clip clip = AudioSystem.getClip()) {
    
    
                clip.open(ais);
                clip.start();
            } catch (UnsupportedAudioFileException | IOException | LineUnavailableException ex) {
                Logger.getLogger(TaskAppuntamento.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
    
    
            }
    Ottengo un'eccezione che mi dice che non è possibile ottenere l'audioinputstream per questo file.

  2. #2
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Codice trovato in internet, ho solo scritto un main per fare una prova (e va)
    codice:
    import java.io.File;
    import java.io.IOException;
    
    
    import javax.sound.sampled.AudioFormat;
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.DataLine;
    import javax.sound.sampled.LineUnavailableException;
    import javax.sound.sampled.SourceDataLine;
    
    
    public class WAVPlayer {
        
        private final int BUFFER_SIZE = 128000;
        private File soundFile;
        private AudioInputStream audioStream;
        private AudioFormat audioFormat;
        private SourceDataLine sourceLine;
    
    
        /**
         * @param filename the name of the file that is going to be played
         */
        
        public void playSound(String filename) {
            
            String strFilename = filename;
            
            try {
                soundFile = new File(strFilename);
            } 
            catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
            
            try {
                audioStream = AudioSystem.getAudioInputStream(soundFile);
            } 
            catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
    
    
            audioFormat = audioStream.getFormat();
    
    
            DataLine.Info info = new DataLine.Info(SourceDataLine.class, audioFormat);
            try {
                sourceLine = (SourceDataLine) AudioSystem.getLine(info);
                sourceLine.open(audioFormat);
            }
            catch (LineUnavailableException e) {
                e.printStackTrace();
                System.exit(1);
            } 
            catch (Exception e) {
                e.printStackTrace();
                System.exit(1);
            }
            
            sourceLine.start();
            
            int nBytesRead = 0;
            byte[] abData = new byte[BUFFER_SIZE];
            while (nBytesRead != -1) {
                try {
                    nBytesRead = audioStream.read(abData, 0, abData.length);
                } 
                catch (IOException e) {
                    e.printStackTrace();
                }
                if (nBytesRead >= 0) {
                    @SuppressWarnings("unused")
                            int nBytesWritten = sourceLine.write(abData, 0, nBytesRead);
                }
            }
            
            sourceLine.drain();
            sourceLine.close();
        }
        
        public static void main (String[] args) {
            (new WAVPlayer()).playSound("sound.wav");
        }
    }
    il metodo l'ho pescato con una ricerca in internet da stackoverflow, per correttezza e per tuo riferimento futuro, linko qui il thread originale:
    http://stackoverflow.com/questions/2...iles-with-java

    Comunque, qui trovi tutto (e di più)
    http://www.jsresources.org/
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

  3. #3
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305
    magari anche qualche tutorial che spiega come utilizzare la riproduzione di suoni in java no applet però

  4. #4
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,254
    Quote Originariamente inviata da linux_r Visualizza il messaggio
    magari anche qualche tutorial che spiega come utilizzare la riproduzione di suoni in java no applet però
    Trail: Sound sul tutorial ufficiale Oracle.
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

  5. #5
    Utente di HTML.it
    Registrato dal
    Jan 2014
    Messaggi
    305
    Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file

    codice:
    File F=new File("becareful.wav");
            AudioInputStream ais=AudioSystem.getAudioInputStream(F);
    l'errore si verifica quando provo ad ottenere l'audioinputstream

  6. #6
    Utente di HTML.it L'avatar di andbin
    Registrato dal
    Jan 2006
    residenza
    Italy
    Messaggi
    18,254
    Quote Originariamente inviata da linux_r Visualizza il messaggio
    Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input file
    Presumo allora che il wav non sia in un formato che è riconosciuto "di serie" dalla Sound API. Il formato wav può contenere svariate cose, con vari tipi di formati, modalità di compressione, ecc...
    Andrea, andbin.devSenior Java developerSCJP 5 (91%) • SCWCD 5 (94%)
    Java Versions Cheat Sheet

  7. #7
    Utente di HTML.it
    Registrato dal
    Aug 2002
    Messaggi
    8,013
    Posta il file
    <´¯)(¯`¤._)(¯`»ANDREA«´¯)(_.¤´¯)(¯`>
    "The answer to your question is: welcome to tomorrow"

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