🥰 Compleate the audio system yooo 🔊🤍

This commit is contained in:
Anas Elgarhy 2022-08-20 09:52:12 +02:00
parent 2803f99a19
commit ebfcf9cf32
10 changed files with 135 additions and 40 deletions

View file

@ -4,7 +4,7 @@
<component name="FrameworkDetectionExcludesConfiguration"> <component name="FrameworkDetectionExcludesConfiguration">
<file type="web" url="file://$PROJECT_DIR$" /> <file type="web" url="file://$PROJECT_DIR$" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" default="true" project-jdk-name="17" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_11" project-jdk-name="17" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" /> <output url="file://$PROJECT_DIR$/out" />
</component> </component>
</project> </project>

View file

@ -14,6 +14,12 @@ repositories {
dependencies { dependencies {
implementation("com.github.anas-elgarhy:alquran-cloud-api:0.4.0-v1") implementation("com.github.anas-elgarhy:alquran-cloud-api:0.4.0-v1")
implementation("com.miglayout:miglayout-swing:11.0") implementation("com.miglayout:miglayout-swing:11.0")
implementation("com.github.goxr3plus:java-stream-player:10.0.2")
implementation("com.googlecode.soundlibs:jlayer:1.0.1.4")
}
java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
} }
// Configure Gradle IntelliJ Plugin // Configure Gradle IntelliJ Plugin

View file

@ -23,7 +23,7 @@ public class AyahStartupActivity implements StartupActivity {
if (basmalhOnStartSettingsState.isActive()) { if (basmalhOnStartSettingsState.isActive()) {
try { try {
final var bassmalh = Ayah.getAyah(1, final var bassmalh = Ayah.getAyah(1,
basmalhOnStartSettingsState.getPlayerId()); basmalhOnStartSettingsState.getEditionId());
NotificationGroupManager.getInstance() NotificationGroupManager.getInstance()
.getNotificationGroup("Basmalh on Start") .getNotificationGroup("Basmalh on Start")
.createNotification(bassmalh.getText(), NotificationType.INFORMATION).notify(project); .createNotification(bassmalh.getText(), NotificationType.INFORMATION).notify(project);

View file

@ -28,18 +28,24 @@ public class NotificationTimerTask extends TimerTask {
public void run() { public void run() {
final var settings = AyahSettingsState.getInstance(); final var settings = AyahSettingsState.getInstance();
LOGGER.info("Player id: " + settings.getEditionId());
try { try {
final var randomAyah = Ayah.getRandomAyah(); final var randomAyah = Ayah.getRandomAyah(settings.getEditionId());
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); randomAyah.getSurah().getName(), randomAyah.getText(), NotificationType.INFORMATION);
notification.addAction(new AnAction("Play") { notification.addAction(new AnAction("Play") {
@Override @Override
public void actionPerformed(@NotNull final AnActionEvent e) { public void actionPerformed(@NotNull final AnActionEvent e) {
LOGGER.info("Play action performed"); LOGGER.info("Play action performed");
new AudioPlayer(settings.getVolume(), randomAyah.getAudioUrl()).play(); LOGGER.info("Audio url: " + randomAyah.getAudioUrl());
play(settings.getVolume(), randomAyah.getAudioUrl());
} }
}); });
@ -65,14 +71,17 @@ public class NotificationTimerTask extends TimerTask {
// Play sound if enabled. // Play sound if enabled.
if (settings.isAutoPlayAudio()) { if (settings.isAutoPlayAudio()) {
LOGGER.info("Playing ayah"); LOGGER.info("Playing ayah");
new AudioPlayer(settings.getVolume(), randomAyah.getAudioUrl()).play(); play(settings.getVolume(), randomAyah.getAudioUrl());
} }
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.severe(e.getMessage()); LOGGER.severe(e.getMessage());
} }
} }
private void play(final int volume, final String audioUrl) {
new AudioPlayer(volume, audioUrl).play();
}
public void setProject(final Project project) { public void setProject(final Project project) {
this.project = project; this.project = project;
} }

View file

@ -1,13 +1,61 @@
package com.anas.intellij.plugins.ayah.audio; package com.anas.intellij.plugins.ayah.audio;
import com.goxr3plus.streamplayer.stream.StreamPlayer;
import com.goxr3plus.streamplayer.stream.StreamPlayerException;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
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 AudioPlayer { public class AudioPlayer {
private final StreamPlayer streamPlayer;
private final String audioUrl;
private static final Logger LOGGER = Logger.getLogger(AudioPlayer.class.getName());
public AudioPlayer(final int volume, final String audioUrl) { public AudioPlayer(final int volume, final String audioUrl) {
streamPlayer = new StreamPlayer();
this.audioUrl = audioUrl;
streamPlayer.setGain(volume / 100.0);
}
private void loadAndOpen() {
try {
streamPlayer.open(getInputStream(audioUrl));
} catch (final MalformedURLException | StreamPlayerException e) {
LOGGER.severe("Error while opening stream player: " + e.getMessage());
} catch (final IOException e) {
LOGGER.severe("Can't load audio file: " + audioUrl);
LOGGER.severe(e.getMessage());
}
} }
public void play() { public void play() {
new Thread(() -> {
try {
loadAndOpen();
streamPlayer.play();
} catch (final StreamPlayerException e) {
LOGGER.severe(e.getMessage());
}
}).start();
}
private InputStream getInputStream(final String audioUrl) throws IOException {
final var url = new URL(audioUrl);
final var inputStream = url.openStream();
return new BufferedInputStream(inputStream);
}
public void run() {
loadAndOpen();
play();
} }
} }

View file

@ -38,7 +38,7 @@ public class AyahSettingsConfigurable implements Configurable {
settingsState.setBasmalhOnStart(settingsComponent.getBasmalhOnStart()); settingsState.setBasmalhOnStart(settingsComponent.getBasmalhOnStart());
settingsState.setIntervalTimeBetweenNotifications(settingsComponent.getIntervalTimeBetweenNotifications()); settingsState.setIntervalTimeBetweenNotifications(settingsComponent.getIntervalTimeBetweenNotifications());
settingsState.setAutoPlayAudio(settingsComponent.isAutoPlayAudio()); settingsState.setAutoPlayAudio(settingsComponent.isAutoPlayAudio());
settingsState.setPlayerId(settingsComponent.getPlayerId()); settingsState.setEditionId(settingsComponent.getEdition().getIdentifier());
settingsState.setVolume(settingsComponent.getVolume()); settingsState.setVolume(settingsComponent.getVolume());
// Update the timer with the new interval time between notifications if interval time between notifications has changed // Update the timer with the new interval time between notifications if interval time between notifications has changed

View file

@ -24,7 +24,7 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
private BasmalhOnStart basmalhOnStart; private BasmalhOnStart basmalhOnStart;
private int intervalTimeBetweenNotifications; // in minutes private int intervalTimeBetweenNotifications; // in minutes
private boolean autoPlayAudio; private boolean autoPlayAudio;
private String playerId; private String editionId;
private int volume; private int volume;
public static AyahSettingsState getInstance() { public static AyahSettingsState getInstance() {
@ -36,9 +36,9 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
intervalTimeBetweenNotifications = 30; // 30 minutes intervalTimeBetweenNotifications = 30; // 30 minutes
autoPlayAudio = false; autoPlayAudio = false;
try { try {
playerId = Edition.getRandomEdition(EditionFormat.AUDIO, "ar").getIdentifier(); editionId = Edition.getRandomEdition(EditionFormat.AUDIO, "ar").getIdentifier();
} catch (final IOException e) { } catch (final IOException e) {
playerId = null; editionId = null;
} }
volume = 40; // 40% volume = 40; // 40%
} }
@ -78,12 +78,12 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
this.autoPlayAudio = autoPlayAudio; this.autoPlayAudio = autoPlayAudio;
} }
public String getPlayerId() { public String getEditionId() {
return playerId; return editionId;
} }
public void setPlayerId(final String playerId) { public void setEditionId(final String editionId) {
this.playerId = playerId; this.editionId = editionId;
} }
public int getVolume() { public int getVolume() {

View file

@ -13,7 +13,7 @@ public class BasmalhOnStart {
private boolean isActive; private boolean isActive;
private boolean isNotificationActive; private boolean isNotificationActive;
private boolean isSoundActive; private boolean isSoundActive;
private String playerId; private String editionId;
private int volume; private int volume;
public BasmalhOnStart() { public BasmalhOnStart() {
@ -21,9 +21,9 @@ public class BasmalhOnStart {
isNotificationActive = true; isNotificationActive = true;
isSoundActive = false; isSoundActive = false;
try { try {
playerId = Edition.getRandomEdition(EditionFormat.AUDIO, "ar").getIdentifier(); editionId = Edition.getRandomEdition(EditionFormat.AUDIO, "ar").getIdentifier();
} catch (final IOException e) { } catch (final IOException e) {
playerId = null; editionId = null;
} }
volume = 40; // 40% volume = 40; // 40%
} }
@ -52,12 +52,12 @@ public class BasmalhOnStart {
isSoundActive = soundActive; isSoundActive = soundActive;
} }
public String getPlayerId() { public String getEditionId() {
return playerId; return editionId;
} }
public void setPlayerId(final String playerId) { public void setEditionId(final String editionId) {
this.playerId = playerId; this.editionId = editionId;
} }
public int getVolume() { public int getVolume() {

View file

@ -0,0 +1,28 @@
package com.anas.intellij.plugins.ayah.settings;
import com.anas.alqurancloudapi.edition.Edition;
/**
* @author: <a href="https://github.com/anas-elgarhy">Anas Elgarhy</a>
* @date: 8/20/22
*/
public class ReadableEdition {
private final Edition edition;
public ReadableEdition(final Edition edition) {
this.edition = edition;
}
public ReadableEdition(final String identifier) {
this.edition = new Edition(identifier);
}
public Edition getEdition() {
return edition;
}
@Override
public String toString() {
return edition.getName() + " (" + edition.getLanguage() + ")";
}
}

View file

@ -12,6 +12,7 @@ import net.miginfocom.swing.MigLayout;
import javax.swing.*; import javax.swing.*;
import java.io.IOException; import java.io.IOException;
import java.util.Objects; import java.util.Objects;
import java.util.logging.Logger;
/** /**
* The settings UI. * The settings UI.
@ -23,14 +24,17 @@ public class SettingsComponent {
private final JPanel panel; private final JPanel panel;
private final JBCheckBox basmalhOnStartCheckBox; private final JBCheckBox basmalhOnStartCheckBox;
private final JBCheckBox autoPlayBasmalhCheckBox; private final JBCheckBox autoPlayBasmalhCheckBox;
private final JComboBox<String> basmalhPlayerIdComboBox; private final JComboBox<ReadableEdition> basmalhPlayerIdComboBox;
private final JBSlider basmalhVolumeSlider; private final JBSlider basmalhVolumeSlider;
private final JSpinner notificationsIntervalSpinner; private final JSpinner notificationsIntervalSpinner;
private SpinnerNumberModel notificationsIntervalSpinnerModel; private SpinnerNumberModel notificationsIntervalSpinnerModel;
private final JBCheckBox notificationsAudioCheckBox; private final JBCheckBox notificationsAudioCheckBox;
private final JComboBox<String> ayahPlayerIdComboBox; private final JComboBox<ReadableEdition> ayahPlayerIdComboBox;
private final JBSlider ayahVolumeSlider; private final JBSlider ayahVolumeSlider;
private final Logger LOGGER = Logger.getLogger(SettingsComponent.class.getName());
// Initialize block yoo.
{ {
basmalhOnStartCheckBox = new JBCheckBox("Basmalh on start"); basmalhOnStartCheckBox = new JBCheckBox("Basmalh on start");
autoPlayBasmalhCheckBox = new JBCheckBox("Auto play basmalh audio"); autoPlayBasmalhCheckBox = new JBCheckBox("Auto play basmalh audio");
@ -103,11 +107,11 @@ public class SettingsComponent {
ayahVolumeSlider.setMajorTickSpacing(20); ayahVolumeSlider.setMajorTickSpacing(20);
ayahVolumeSlider.setMinorTickSpacing(10); ayahVolumeSlider.setMinorTickSpacing(10);
if (settings.getBasmalhOnStart().getPlayerId() != null) { if (settings.getBasmalhOnStart().getEditionId() != null) {
basmalhPlayerIdComboBox.setSelectedItem(settings.getBasmalhOnStart().getPlayerId()); basmalhPlayerIdComboBox.setSelectedItem(new ReadableEdition(settings.getBasmalhOnStart().getEditionId()));
} }
if (settings.getPlayerId() != null) { if (settings.getEditionId() != null) {
ayahPlayerIdComboBox.setSelectedItem(settings.getPlayerId()); ayahPlayerIdComboBox.setSelectedItem(new ReadableEdition(settings.getEditionId()));
} }
} }
@ -126,22 +130,22 @@ public class SettingsComponent {
try { try {
final var editions = Edition.getEditions(EditionFormat.AUDIO); final var editions = Edition.getEditions(EditionFormat.AUDIO);
for (final var edition : editions) { for (final var edition : editions) {
basmalhPlayerIdComboBox.addItem(edition.getEnglishName()); basmalhPlayerIdComboBox.addItem(new ReadableEdition(edition));
ayahPlayerIdComboBox.addItem(edition.getEnglishName()); ayahPlayerIdComboBox.addItem(new ReadableEdition(edition));
} }
} catch (final IOException e) { } catch (final IOException e) {
e.printStackTrace(); LOGGER.severe(e.getMessage());
basmalhPlayerIdComboBox.addItem("Error can't get editions, please check internet connection");
ayahPlayerIdComboBox.addItem("Error can't get editions, please check internet connection");
} }
} }
public boolean isModified() { public boolean isModified() {
final var settings = AyahSettingsState.getInstance(); final var settings = AyahSettingsState.getInstance();
return settings.getBasmalhOnStart().getPlayerId() != null && return settings.getBasmalhOnStart().getEditionId() != null &&
!settings.getBasmalhOnStart().getPlayerId().equals(basmalhPlayerIdComboBox.getSelectedItem()) || !settings.getBasmalhOnStart().getEditionId()
settings.getPlayerId() != null && .equals(((ReadableEdition) basmalhPlayerIdComboBox.getSelectedItem()).getEdition()) ||
!settings.getPlayerId().equals(ayahPlayerIdComboBox.getSelectedItem()) || settings.getEditionId() != null &&
!settings.getEditionId()
.equals(((ReadableEdition) ayahPlayerIdComboBox.getSelectedItem()).getEdition()) ||
settings.getIntervalTimeBetweenNotifications() != notificationsIntervalSpinnerModel.getNumber().intValue() || settings.getIntervalTimeBetweenNotifications() != notificationsIntervalSpinnerModel.getNumber().intValue() ||
settings.getBasmalhOnStart().isActive() != basmalhOnStartCheckBox.isSelected() || settings.getBasmalhOnStart().isActive() != basmalhOnStartCheckBox.isSelected() ||
settings.getBasmalhOnStart().isSoundActive() != autoPlayBasmalhCheckBox.isSelected() || settings.getBasmalhOnStart().isSoundActive() != autoPlayBasmalhCheckBox.isSelected() ||
@ -162,7 +166,7 @@ public class SettingsComponent {
final var b = new BasmalhOnStart(); final var b = new BasmalhOnStart();
b.setActive(basmalhOnStartCheckBox.isSelected()); b.setActive(basmalhOnStartCheckBox.isSelected());
b.setSoundActive(autoPlayBasmalhCheckBox.isSelected()); b.setSoundActive(autoPlayBasmalhCheckBox.isSelected());
b.setPlayerId(Objects.requireNonNull(basmalhPlayerIdComboBox.getSelectedItem()).toString()); b.setEditionId(((ReadableEdition) Objects.requireNonNull(basmalhPlayerIdComboBox.getSelectedItem())).getEdition().getIdentifier());
b.setVolume(basmalhVolumeSlider.getValue()); b.setVolume(basmalhVolumeSlider.getValue());
return b; return b;
} }
@ -175,8 +179,8 @@ public class SettingsComponent {
return notificationsAudioCheckBox.isSelected(); return notificationsAudioCheckBox.isSelected();
} }
public String getPlayerId() { public Edition getEdition() {
return Objects.requireNonNull(ayahPlayerIdComboBox.getSelectedItem()).toString(); return ((ReadableEdition) ayahPlayerIdComboBox.getSelectedItem()).getEdition();
} }
public int getVolume() { public int getVolume() {