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