Add stop function to the play action 🥰

This commit is contained in:
Anas Elgarhy 2022-08-22 15:28:28 +02:00
parent a9803f31ff
commit 2c52408530

View file

@ -2,6 +2,7 @@ package com.anas.intellij.plugins.ayah;
import com.anas.alqurancloudapi.Ayah; import com.anas.alqurancloudapi.Ayah;
import com.anas.intellij.plugins.ayah.audio.AudioPlayer; import com.anas.intellij.plugins.ayah.audio.AudioPlayer;
import com.anas.intellij.plugins.ayah.audio.PlayerListener;
import com.anas.intellij.plugins.ayah.dialogs.AyahDetailsDialog; import com.anas.intellij.plugins.ayah.dialogs.AyahDetailsDialog;
import com.anas.intellij.plugins.ayah.settings.AyahSettingsState; import com.anas.intellij.plugins.ayah.settings.AyahSettingsState;
import com.intellij.notification.Notification; import com.intellij.notification.Notification;
@ -9,6 +10,7 @@ import com.intellij.notification.NotificationType;
import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import javazoom.jl.player.advanced.PlaybackEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.awt.*; import java.awt.*;
@ -21,9 +23,13 @@ import java.util.logging.Logger;
* @author: <a href="https://github.com/anas-elgarhy">Anas Elgarhy</a> * @author: <a href="https://github.com/anas-elgarhy">Anas Elgarhy</a>
* @date: 8/19/22 * @date: 8/19/22
*/ */
public class NotificationTimerTask extends TimerTask { public class NotificationTimerTask extends TimerTask implements PlayerListener {
private Project project; private Project project;
private static final Logger LOGGER = Logger.getLogger(NotificationTimerTask.class.getName()); private static final Logger LOGGER = Logger.getLogger(NotificationTimerTask.class.getName());
private AudioPlayer audioPlayer;
private boolean isPlaying;
private Ayah ayah;
@Override @Override
public void run() { public void run() {
@ -31,31 +37,28 @@ public class NotificationTimerTask extends TimerTask {
LOGGER.info("Player id: " + settings.getEdition()); LOGGER.info("Player id: " + settings.getEdition());
try { try {
final var randomAyah = Ayah.getRandomAyah(settings.getEdition().getEditionIdentifier()); ayah = Ayah.getRandomAyah(settings.getEdition().getEditionIdentifier());
LOGGER.info("Random Ayah: " + randomAyah.getText());
LOGGER.info("Rsndom ayah edition: " + randomAyah.getEdition());
LOGGER.info("Random Ayah Url: " + randomAyah.getAudioUrl());
// Set up the notification. // Set up the notification.
final var notification = new Notification("Random Ayah Notification", final var notification = new Notification("Random Ayah Notification",
randomAyah.getSurah().getName(), randomAyah.getText(), NotificationType.INFORMATION); ayah.getSurah().getName(), ayah.getText(), NotificationType.INFORMATION);
notification.addAction(new AnAction("Play") { // Play sound if enabled.
@Override if (settings.isAutoPlayAudio()) {
public void actionPerformed(@NotNull final AnActionEvent e) { LOGGER.info("Playing ayah");
LOGGER.info("Play action performed"); audioPlayer = new AudioPlayer(ayah.getAudioUrl()).setListener(this);
LOGGER.info("Audio url: " + randomAyah.getAudioUrl()); audioPlayer.play();
play(randomAyah.getAudioUrl()); }
}
}); // Setup the notification actions.
notification.addAction(new PlayAction());
notification.addAction(new AnAction("Copy") { notification.addAction(new AnAction("Copy") {
@Override @Override
public void actionPerformed(@NotNull final AnActionEvent e) { public void actionPerformed(@NotNull final AnActionEvent e) {
LOGGER.info("Copy action performed"); LOGGER.info("Copy action performed");
final var clipboard = Toolkit.getDefaultToolkit().getSystemClipboard(); final var clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
clipboard.setContents(new StringSelection(randomAyah.getText()), null); clipboard.setContents(new StringSelection(ayah.getText()), null);
} }
}); });
@ -63,28 +66,49 @@ public class NotificationTimerTask extends TimerTask {
@Override @Override
public void actionPerformed(@NotNull final AnActionEvent e) { public void actionPerformed(@NotNull final AnActionEvent e) {
LOGGER.info("Details action performed"); LOGGER.info("Details action performed");
new AyahDetailsDialog(randomAyah).setVisible(true); new AyahDetailsDialog(ayah).setVisible(true);
} }
}); });
// Show notification // Show notification
notification.notify(project); notification.notify(project);
// Play sound if enabled.
if (settings.isAutoPlayAudio()) {
LOGGER.info("Playing ayah");
play(randomAyah.getAudioUrl());
}
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.severe(e.getMessage()); LOGGER.severe(e.getMessage());
} }
} }
private void play(final String audioUrl) {
new AudioPlayer(audioUrl).play();
}
public void setProject(final Project project) { public void setProject(final Project project) {
this.project = project; this.project = project;
} }
// Play action implementation.
private class PlayAction extends AnAction {
PlayAction() {
super("Play", "Play the ayah", null);
}
@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
LOGGER.info("Play action performed");
if (!isPlaying) {
audioPlayer = new AudioPlayer(ayah.getAudioUrl()).setListener(NotificationTimerTask.this);
audioPlayer.play();
} else {
audioPlayer.stop();
isPlaying = false;
}
}
}
// Player listener methods.
@Override
public void onStarted(final PlaybackEvent event) {
isPlaying = true;
}
@Override
public void onFinished(PlaybackEvent event) {
isPlaying = false;
}
} }