Changeset 3962 in osm for applications/editors/josm/plugins/surveyor/src/at
- Timestamp:
- 2007-08-05T21:54:03+02:00 (17 years ago)
- Location:
- applications/editors/josm/plugins/surveyor/src/at/dallermassl/josm/plugin/surveyor
- Files:
-
- 2 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/surveyor/src/at/dallermassl/josm/plugin/surveyor/action/PlayAudioAction.java
r3348 r3962 4 4 package at.dallermassl.josm.plugin.surveyor.action; 5 5 6 import java.io. File;6 import java.io.BufferedInputStream; 7 7 import java.io.IOException; 8 import java.io.InputStream; 8 9 import java.net.MalformedURLException; 9 10 … … 19 20 20 21 import at.dallermassl.josm.plugin.surveyor.GpsActionEvent; 22 import at.dallermassl.josm.plugin.surveyor.util.ResourceLoader; 21 23 22 24 /** … … 27 29 */ 28 30 public class PlayAudioAction extends AbstractSurveyorAction { 29 private File audioFile = null;31 private String audioSource = null; 30 32 31 33 /* (non-Javadoc) … … 34 36 //@Override 35 37 public void actionPerformed(GpsActionEvent event) { 36 try { 37 if(audioFile == null) { 38 audioFile = new File(getParameters().get(0)); 39 if(!audioFile.exists()) { 40 audioFile = new File(Main.pref.getPreferencesDir(), getParameters().get(0)); 41 if(!audioFile.exists()) { 42 System.err.println("Audio file " + getParameters().get(0) + " not found!"); 43 return; 38 // run as a separate thread 39 Main.worker.execute(new Runnable() { 40 public void run() { 41 try { 42 if(audioSource == null) { 43 audioSource = getParameters().get(0); 44 System.out.println("reading audio from " + audioSource); 44 45 } 46 InputStream in = new BufferedInputStream(ResourceLoader.getInputStream(audioSource)); 47 AudioInputStream stream = AudioSystem.getAudioInputStream(in); 48 49 // From URL 50 // stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile")); 51 52 // At present, ALAW and ULAW encodings must be converted 53 // to PCM_SIGNED before it can be played 54 AudioFormat format = stream.getFormat(); 55 if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { 56 format = new AudioFormat( 57 AudioFormat.Encoding.PCM_SIGNED, 58 format.getSampleRate(), 59 format.getSampleSizeInBits()*2, 60 format.getChannels(), 61 format.getFrameSize()*2, 62 format.getFrameRate(), 63 true); // big endian 64 stream = AudioSystem.getAudioInputStream(format, stream); 65 } 66 67 // Create the clip 68 DataLine.Info info = new DataLine.Info( 69 Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize())); 70 Clip clip = (Clip) AudioSystem.getLine(info); 71 72 // This method does not return until the audio file is completely loaded 73 clip.open(stream); 74 75 // Start playing 76 clip.start(); 77 } catch (MalformedURLException e) { 78 e.printStackTrace(); 79 } catch (IOException e) { 80 e.printStackTrace(); 81 } catch (LineUnavailableException e) { 82 e.printStackTrace(); 83 } catch (UnsupportedAudioFileException e) { 84 e.printStackTrace(); 45 85 } 46 86 } 47 // From file 48 AudioInputStream stream = AudioSystem.getAudioInputStream(audioFile); 49 50 // From URL 51 // stream = AudioSystem.getAudioInputStream(new URL("http://hostname/audiofile")); 52 53 // At present, ALAW and ULAW encodings must be converted 54 // to PCM_SIGNED before it can be played 55 AudioFormat format = stream.getFormat(); 56 if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { 57 format = new AudioFormat( 58 AudioFormat.Encoding.PCM_SIGNED, 59 format.getSampleRate(), 60 format.getSampleSizeInBits()*2, 61 format.getChannels(), 62 format.getFrameSize()*2, 63 format.getFrameRate(), 64 true); // big endian 65 stream = AudioSystem.getAudioInputStream(format, stream); 66 } 67 68 // Create the clip 69 DataLine.Info info = new DataLine.Info( 70 Clip.class, stream.getFormat(), ((int)stream.getFrameLength()*format.getFrameSize())); 71 Clip clip = (Clip) AudioSystem.getLine(info); 72 73 // This method does not return until the audio file is completely loaded 74 clip.open(stream); 75 76 // Start playing 77 clip.start(); 78 } catch (MalformedURLException e) { 79 e.printStackTrace(); 80 } catch (IOException e) { 81 e.printStackTrace(); 82 } catch (LineUnavailableException e) { 83 e.printStackTrace(); 84 } catch (UnsupportedAudioFileException e) { 85 e.printStackTrace(); 86 } 87 88 }); 87 89 88 90 }
Note:
See TracChangeset
for help on using the changeset viewer.