X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=tim%2Fprune%2Ffunction%2FPlayAudioFunction.java;h=0e14d7c9ad1f9f6c9f4df9f761b7c466838b751d;hp=7e7a34a8ab56d05af8140f75d4e6def27d6f1c35;hb=649c5da6ee1bbc590699e11a92316ece2ea8512d;hpb=eebbb64b5d63f9eea43a0dff908c30361a376768 diff --git a/tim/prune/function/PlayAudioFunction.java b/tim/prune/function/PlayAudioFunction.java index 7e7a34a..0e14d7c 100644 --- a/tim/prune/function/PlayAudioFunction.java +++ b/tim/prune/function/PlayAudioFunction.java @@ -1,5 +1,6 @@ package tim.prune.function; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import javax.sound.sampled.AudioInputStream; @@ -8,9 +9,10 @@ import javax.sound.sampled.Clip; import tim.prune.App; import tim.prune.GenericFunction; +import tim.prune.data.AudioClip; /** - * Class to play the current audio file + * Class to play the current audio clip */ public class PlayAudioFunction extends GenericFunction implements Runnable { @@ -49,12 +51,13 @@ public class PlayAudioFunction extends GenericFunction implements Runnable */ public void run() { - File audioFile = _app.getTrackInfo().getCurrentAudio().getFile(); + AudioClip audio = _app.getTrackInfo().getCurrentAudio(); + File audioFile = audio.getFile(); boolean played = false; - if (audioFile.exists() && audioFile.isFile() && audioFile.canRead()) + if (audioFile != null && audioFile.exists() && audioFile.isFile() && audioFile.canRead()) { // First choice is to play using java - played = playClip(audioFile); + played = playClip(audio); // Second choice is to try the Desktop library from java 6, if available if (!played) { try { @@ -84,6 +87,10 @@ public class PlayAudioFunction extends GenericFunction implements Runnable } } } + else if (audioFile == null && audio.getByteData() != null) { + // Try to play audio clip using byte array (can't use Desktop or Runtime) + played = playClip(audio); + } if (!played) { // If still not worked, show error message @@ -93,16 +100,21 @@ public class PlayAudioFunction extends GenericFunction implements Runnable /** * Try to play the sound file using built-in java libraries + * @param inAudio audio clip to play * @return true if play was successful */ - private boolean playClip(File inFile) + private boolean playClip(AudioClip inClip) { boolean success = false; AudioInputStream audioInputStream = null; _clip = null; try { - audioInputStream = AudioSystem.getAudioInputStream(inFile); + if (inClip.getFile() != null) + audioInputStream = AudioSystem.getAudioInputStream(inClip.getFile()); + else if (inClip.getByteData() != null) + audioInputStream = AudioSystem.getAudioInputStream(new ByteArrayInputStream(inClip.getByteData())); + else return false; _clip = AudioSystem.getClip(); _clip.open(audioInputStream); // play the clip @@ -110,6 +122,7 @@ public class PlayAudioFunction extends GenericFunction implements Runnable _clip.drain(); success = true; } catch (Exception e) { + System.err.println(e.getClass().getName() + " - " + e.getMessage()); } finally { // close the stream to clean up try {