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