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