001    // License: GPL. Copyright 2007 by Immanuel Scholz and others
002    package org.openstreetmap.josm.gui.layer.markerlayer;
003    
004    import java.awt.event.ActionEvent;
005    import java.net.URL;
006    
007    import org.openstreetmap.josm.Main;
008    import org.openstreetmap.josm.data.coor.LatLon;
009    import org.openstreetmap.josm.tools.AudioPlayer;
010    import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
011    
012    /**
013     * Marker class with audio playback capability.
014     *
015     * @author Frederik Ramm <frederik@remote.org>
016     *
017     */
018    public class AudioMarker extends ButtonMarker {
019    
020        private URL audioUrl;
021        private static AudioMarker recentlyPlayedMarker = null;
022        public double syncOffset;
023        public boolean timeFromAudio = false; // as opposed to from the GPX track
024    
025        public AudioMarker(LatLon ll, TemplateEngineDataProvider dataProvider, URL audioUrl, MarkerLayer parentLayer, double time, double offset) {
026            super(ll, dataProvider, "speech.png", parentLayer, time, offset);
027            this.audioUrl = audioUrl;
028            this.syncOffset = 0.0;
029            this.timeFromAudio = false;
030        }
031    
032        @Override public void actionPerformed(ActionEvent ev) {
033            play();
034        }
035    
036        public static AudioMarker recentlyPlayedMarker() {
037            return recentlyPlayedMarker;
038        }
039    
040        public URL url() {
041            return audioUrl;
042        }
043    
044        /**
045         * Starts playing the audio associated with the marker offset by the given amount
046         * @param after : seconds after marker where playing should start
047         */
048        public void play(double after) {
049            try {
050                // first enable tracing the audio along the track
051                Main.map.mapView.playHeadMarker.animate();
052    
053                AudioPlayer.play(audioUrl, offset + syncOffset + after);
054                recentlyPlayedMarker = this;
055            } catch (Exception e) {
056                AudioPlayer.audioMalfunction(e);
057            }
058        }
059    
060        /**
061         * Starts playing the audio associated with the marker: used in response to pressing
062         * the marker as well as indirectly
063         *
064         */
065        public void play() { play(0.0); }
066    
067        public void adjustOffset(double adjustment) {
068            syncOffset = adjustment; // added to offset may turn out negative, but that's ok
069        }
070    
071        public double syncOffset() {
072            return syncOffset;
073        }
074    
075        @Override
076        protected TemplateEntryProperty getTextTemplate() {
077            return TemplateEntryProperty.forAudioMarker(parentLayer.getName());
078        }
079    }