Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:java:java_hangok

< Java

Java hangok

Play

Play.java
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.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;
 
public class Play {
    public void playSound() {
        try {
            tryPlaySound();
        } catch (UnsupportedAudioFileException e) {
            System.err.println("Hiba! Nem támogatott formátum!");
            System.err.println(e.getMessage());
        } catch (IOException e) {
            System.err.println("Hiba! Fájl megnyitási probléma!");
            System.err.println(e.getMessage());            
        } catch (LineUnavailableException e) {
            System.err.println("Hiba! Vonali probléma!");
            System.err.println(e.getMessage());
        } catch (InterruptedException e) {
            System.err.println(e.getMessage());
        }
    }
    public void tryPlaySound() 
            throws 
                UnsupportedAudioFileException, 
                IOException, 
                LineUnavailableException, InterruptedException {
        File file = new File("guitar.wav");
        AudioInputStream audio = AudioSystem.getAudioInputStream(file);
        Clip clip = AudioSystem.getClip();
 
        clip.open(audio);
 
        clip.start();
        long duration=getDurationInSec(audio);
 
        Thread.sleep(duration*1000+1000);        
    }
    private static long getDurationInSec(final AudioInputStream audioIn){
        final AudioFormat format=audioIn.getFormat();
        double frameRate=format.getFrameRate();
        return (long)(audioIn.getFrameLength()/frameRate);
    }    
}
App.java
public class App {
    public static void main(String[] args) throws Exception {
        new Play().playSound();
    }
}
oktatas/programozas/java/java_hangok.txt · Utolsó módosítás: 2024/01/02 14:09 szerkesztette: admin