You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AudioPlayer.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.addons.audio;
  18. import java.applet.Applet;
  19. import java.applet.AudioClip;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.net.MalformedURLException;
  23. import javax.sound.sampled.AudioInputStream;
  24. import javax.sound.sampled.AudioSystem;
  25. import javax.sound.sampled.UnsupportedAudioFileException;
  26. /**
  27. * The AudioPlayer handles the playing of the audio
  28. */
  29. public final class AudioPlayer implements Runnable {
  30. /** The AudioType enum */
  31. public enum AudioType {
  32. WAV, INVALID
  33. }
  34. /** The file object of the file to play */
  35. final File myFile;
  36. /**
  37. * Create the AudioPlayer
  38. *
  39. * @param file The file to play
  40. */
  41. public AudioPlayer(final File file) {
  42. myFile = file;
  43. }
  44. /**
  45. * Play this AudioPlayer.
  46. */
  47. public void play() {
  48. new Thread(this).start();
  49. }
  50. /**
  51. * Run this AudioPlayer (Should not be invoked directly).
  52. */
  53. @Override
  54. public void run() {
  55. final AudioType type = getAudioType(myFile);
  56. switch (type) {
  57. case WAV:
  58. playWav();
  59. break;
  60. default:
  61. break;
  62. }
  63. }
  64. /**
  65. * Check if this File is a supported file type
  66. *
  67. * @param file the File to check
  68. *
  69. * @return true if playable, else false.
  70. */
  71. public static boolean isValid(final File file) {
  72. final AudioType type = getAudioType(file);
  73. return type != AudioType.INVALID;
  74. }
  75. /**
  76. * Get the AudioType of a given file
  77. *
  78. * @param file the File to check
  79. *
  80. * @return AudioType for this file.
  81. */
  82. public static AudioType getAudioType(final File file) {
  83. try (AudioInputStream unused = AudioSystem.getAudioInputStream(file)) {
  84. return AudioType.WAV;
  85. } catch (UnsupportedAudioFileException | IOException e) {
  86. return AudioType.INVALID;
  87. }
  88. }
  89. /**
  90. * Play the file as a wav file, using the Applet class. (This code seems to work better than the
  91. * non-applet version, but can't play streams)
  92. */
  93. private void playWav() {
  94. try {
  95. final AudioClip ac = Applet.newAudioClip(myFile.toURI().toURL());
  96. if (ac != null) {
  97. ac.play();
  98. }
  99. } catch (MalformedURLException e) {
  100. }
  101. }
  102. }