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; 016import org.openstreetmap.josm.tools.Utils; 017 018/** 019 * If not playing, play the sound track from the first Audio Marker, or from the point at which it was paused.<br> 020 * If playing, pause the sound.<br> 021 * If fast forwarding or slow forwarding, resume normal speed. 022 * @since 547 023 */ 024public class AudioPlayPauseAction extends JosmAction { 025 026 /** 027 * Constructs a new {@code AudioPlayPauseAction}. 028 */ 029 public AudioPlayPauseAction() { 030 super(trc("audio", "Play/Pause"), "audio-playpause", tr("Play/pause audio."), 031 Shortcut.registerShortcut("audio:pause", tr("Audio: {0}", trc("audio", "Play/Pause")), KeyEvent.VK_PERIOD, Shortcut.DIRECT), true); 032 } 033 034 @Override 035 public void actionPerformed(ActionEvent e) { 036 URL url = AudioPlayer.url(); 037 try { 038 if (AudioPlayer.paused() && url != null) { 039 AudioPlayer.play(url); 040 } else if (AudioPlayer.playing()) { 041 if (!Utils.equalsEpsilon(AudioPlayer.speed(), 1.0)) 042 AudioPlayer.play(url, AudioPlayer.position()); 043 else 044 AudioPlayer.pause(); 045 } else { 046 // play the last-played marker again, if there is one 047 AudioMarker lastPlayed = AudioMarker.recentlyPlayedMarker(); 048 if (lastPlayed != null) { 049 lastPlayed.play(); 050 } else { 051 // If no marker was played recently, play the first one 052 MarkerLayer.playAudio(); 053 } 054 } 055 } catch (Exception ex) { 056 AudioPlayer.audioMalfunction(ex); 057 } 058 } 059}