Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. package com.company;
  2. import javax.sound.sampled.*;
  3. import java.io.File;
  4.  
  5. public class SoundSharedInstance {
  6. private static SoundSharedInstance instance = null;
  7.  
  8. private File file;
  9. private AudioInputStream audioInputStream;
  10. private Clip clip;
  11. private SoundSharedInstance() {
  12. }
  13.  
  14. public static SoundSharedInstance getInstance() {
  15. if (instance == null) {
  16. instance = new SoundSharedInstance();
  17. }
  18. return instance;
  19. }
  20.  
  21. public void createAudio(File file) {
  22. try {
  23. this.file = file;
  24. audioInputStream = AudioSystem.getAudioInputStream(file);
  25. clip = AudioSystem.getClip();
  26. clip.open(audioInputStream);
  27. clip.addLineListener(event -> {
  28. if (event.getType().equals(LineEvent.Type.STOP)) {
  29. startAudio();
  30. }
  31. });
  32. } catch (Exception e) {
  33. System.out.print("Error Starting Audio");
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. public void startAudio() {
  39. clip.start();
  40. }
  41.  
  42. public void stopAudio() {
  43. clip.stop();
  44. }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement