[[oktatas:programozás:java|< Java]]
====== Java hangok ======
* **Szerző:** Sallai András
* Copyright (c) 2024, Sallai András
* Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]]
* Web: https://szit.hu
===== Play =====
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);
}
}
public class App {
public static void main(String[] args) throws Exception {
new Play().playSound();
}
}