Advertisement
Guest User

Untitled

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