001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.actions.audio;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005import static org.openstreetmap.josm.tools.I18n.trc;
006
007import java.awt.event.ActionEvent;
008import java.awt.event.KeyEvent;
009import java.net.URL;
010
011import org.openstreetmap.josm.actions.JosmAction;
012import org.openstreetmap.josm.gui.layer.markerlayer.AudioMarker;
013import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
014import org.openstreetmap.josm.tools.AudioPlayer;
015import org.openstreetmap.josm.tools.Shortcut;
016
017/**
018 * If not playing, play the sound track from the first Audio Marker, or from the point at which it was paused.<br/>
019 * If playing, pause the sound.<br/>
020 * If fast forwarding or slow forwarding, resume normal speed.
021 * @since 547
022 */
023public class AudioPlayPauseAction extends JosmAction {
024
025    /**
026     * Constructs a new {@code AudioPlayPauseAction}.
027     */
028    public AudioPlayPauseAction() {
029        super(trc("audio", "Play/Pause"), "audio-playpause", tr("Play/pause audio."),
030        Shortcut.registerShortcut("audio:pause", tr("Audio: {0}", trc("audio", "Play/Pause")), KeyEvent.VK_PERIOD, Shortcut.DIRECT), true);
031    }
032
033    @Override
034    public void actionPerformed(ActionEvent e) {
035        URL url = AudioPlayer.url();
036        try {
037            if (AudioPlayer.paused() && url != null) {
038                AudioPlayer.play(url);
039            } else if (AudioPlayer.playing()){
040                if (AudioPlayer.speed() != 1.0)
041                    AudioPlayer.play(url, AudioPlayer.position());
042                else
043                    AudioPlayer.pause();
044            } else {
045                // play the last-played marker again, if there is one
046                AudioMarker lastPlayed = AudioMarker.recentlyPlayedMarker();
047                if (lastPlayed != null) {
048                    lastPlayed.play();
049                } else {
050                    // If no marker was played recently, play the first one
051                    MarkerLayer.playAudio();
052                }
053            }
054        } catch (Exception ex) {
055            AudioPlayer.audioMalfunction(ex);
056        }
057    }
058}