1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions.audio;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.trc;
|
---|
6 |
|
---|
7 | import java.awt.event.ActionEvent;
|
---|
8 | import java.awt.event.KeyEvent;
|
---|
9 | import java.net.URL;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
12 | import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker;
|
---|
13 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
14 | import org.openstreetmap.josm.tools.AudioPlayer;
|
---|
15 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
16 | import org.openstreetmap.josm.tools.Utils;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * If not playing, play the sound track from the first Audio Marker, or from the point at which it was paused.<br>
|
---|
20 | * If playing, pause the sound.<br>
|
---|
21 | * If fast forwarding or slow forwarding, resume normal speed.
|
---|
22 | * @since 547
|
---|
23 | */
|
---|
24 | public class AudioPlayPauseAction extends JosmAction {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Constructs a new {@code AudioPlayPauseAction}.
|
---|
28 | */
|
---|
29 | public AudioPlayPauseAction() {
|
---|
30 | super(trc("audio", "Play/Pause"), "audio-playpause", tr("Play/pause audio."),
|
---|
31 | Shortcut.registerShortcut("audio:pause", tr("Audio: {0}", trc("audio", "Play/Pause")), KeyEvent.VK_PERIOD, Shortcut.DIRECT), true);
|
---|
32 | }
|
---|
33 |
|
---|
34 | @Override
|
---|
35 | public void actionPerformed(ActionEvent e) {
|
---|
36 | URL url = AudioPlayer.url();
|
---|
37 | try {
|
---|
38 | if (AudioPlayer.paused() && url != null) {
|
---|
39 | AudioPlayer.play(url);
|
---|
40 | } else if (AudioPlayer.playing()) {
|
---|
41 | if (!Utils.equalsEpsilon(AudioPlayer.speed(), 1.0))
|
---|
42 | AudioPlayer.play(url, AudioPlayer.position());
|
---|
43 | else
|
---|
44 | AudioPlayer.pause();
|
---|
45 | } else {
|
---|
46 | // play the last-played marker again, if there is one
|
---|
47 | AudioMarker lastPlayed = AudioMarker.recentlyPlayedMarker();
|
---|
48 | if (lastPlayed != null) {
|
---|
49 | lastPlayed.play();
|
---|
50 | } else {
|
---|
51 | // If no marker was played recently, play the first one
|
---|
52 | MarkerLayer.playAudio();
|
---|
53 | }
|
---|
54 | }
|
---|
55 | } catch (Exception ex) {
|
---|
56 | AudioPlayer.audioMalfunction(ex);
|
---|
57 | }
|
---|
58 | }
|
---|
59 | }
|
---|