source: josm/trunk/src/org/openstreetmap/josm/gui/layer/markerlayer/AudioMarker.java@ 13419

Last change on this file since 13419 was 13419, checked in by Don-vip, 6 years ago

fix #14802 - make sure audio resources are cleared when marker layer is destroyed

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.markerlayer;
3
4import java.awt.event.ActionEvent;
5import java.io.IOException;
6import java.net.URL;
7import java.util.Collections;
8
9import org.openstreetmap.josm.data.coor.LatLon;
10import org.openstreetmap.josm.data.gpx.GpxConstants;
11import org.openstreetmap.josm.data.gpx.GpxLink;
12import org.openstreetmap.josm.data.gpx.WayPoint;
13import org.openstreetmap.josm.gui.MainApplication;
14import org.openstreetmap.josm.io.audio.AudioPlayer;
15import org.openstreetmap.josm.io.audio.AudioUtil;
16import org.openstreetmap.josm.tools.template_engine.TemplateEngineDataProvider;
17
18/**
19 * Marker class with audio playback capability.
20 *
21 * @author Frederik Ramm
22 *
23 */
24public class AudioMarker extends ButtonMarker {
25
26 private final URL audioUrl;
27 private static volatile AudioMarker recentlyPlayedMarker;
28 public double syncOffset;
29 public boolean timeFromAudio; // as opposed to from the GPX track
30
31 public AudioMarker(LatLon ll, TemplateEngineDataProvider dataProvider, URL audioUrl, MarkerLayer parentLayer, double time, double offset) {
32 super(ll, dataProvider, /* ICON(markers/) */ "speech", parentLayer, time, offset);
33 this.audioUrl = audioUrl;
34 this.syncOffset = 0.0;
35 this.timeFromAudio = false;
36 }
37
38 @Override
39 public void actionPerformed(ActionEvent ev) {
40 play();
41 }
42
43 /**
44 * Returns the marker played the most recently, if any.
45 * @return the marker played the most recently, or {@code null}
46 */
47 public static AudioMarker recentlyPlayedMarker() {
48 return recentlyPlayedMarker;
49 }
50
51 /**
52 * Forgets the marker played the most recently, if any.
53 */
54 static void resetRecentlyPlayedMarker() {
55 recentlyPlayedMarker = null;
56 }
57
58 public URL url() {
59 return audioUrl;
60 }
61
62 /**
63 * Starts playing the audio associated with the marker offset by the given amount
64 * @param after : seconds after marker where playing should start
65 */
66 public void play(double after) {
67 try {
68 // first enable tracing the audio along the track
69 MainApplication.getMap().mapView.playHeadMarker.animate();
70
71 AudioPlayer.play(audioUrl, offset + syncOffset + after);
72 recentlyPlayedMarker = this;
73 } catch (IOException | InterruptedException e) {
74 AudioUtil.audioMalfunction(e);
75 }
76 }
77
78 /**
79 * Starts playing the audio associated with the marker: used in response to pressing
80 * the marker as well as indirectly
81 *
82 */
83 public void play() {
84 play(0.0);
85 }
86
87 public void adjustOffset(double adjustment) {
88 syncOffset = adjustment; // added to offset may turn out negative, but that's ok
89 }
90
91 public double syncOffset() {
92 return syncOffset;
93 }
94
95 @Override
96 protected TemplateEntryProperty getTextTemplate() {
97 return TemplateEntryProperty.forAudioMarker(parentLayer.getName());
98 }
99
100 @Override
101 public WayPoint convertToWayPoint() {
102 WayPoint wpt = super.convertToWayPoint();
103 GpxLink link = new GpxLink(audioUrl.toString());
104 link.type = "audio";
105 wpt.put(GpxConstants.META_LINKS, Collections.singleton(link));
106 wpt.addExtension("offset", Double.toString(offset));
107 wpt.addExtension("sync-offset", Double.toString(syncOffset));
108 return wpt;
109 }
110}
Note: See TracBrowser for help on using the repository browser.