🥰 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">
<file type="web" url="file://$PROJECT_DIR$" />
</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" />
</component>
</project>

View file

@ -14,6 +14,12 @@ repositories {
dependencies {
implementation("com.github.anas-elgarhy:alquran-cloud-api:0.4.0-v1")
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

View file

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

View file

@ -28,18 +28,24 @@ public class NotificationTimerTask extends TimerTask {
public void run() {
final var settings = AyahSettingsState.getInstance();
LOGGER.info("Player id: " + settings.getEditionId());
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.
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") {
@Override
public void actionPerformed(@NotNull final AnActionEvent e) {
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.
if (settings.isAutoPlayAudio()) {
LOGGER.info("Playing ayah");
new AudioPlayer(settings.getVolume(), randomAyah.getAudioUrl()).play();
play(settings.getVolume(), randomAyah.getAudioUrl());
}
} catch (final IOException e) {
LOGGER.severe(e.getMessage());
}
}
private void play(final int volume, final String audioUrl) {
new AudioPlayer(volume, audioUrl).play();
}
public void setProject(final Project project) {
this.project = project;
}

View file

@ -1,13 +1,61 @@
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>
* @date: 8/19/22
*/
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) {
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() {
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.setIntervalTimeBetweenNotifications(settingsComponent.getIntervalTimeBetweenNotifications());
settingsState.setAutoPlayAudio(settingsComponent.isAutoPlayAudio());
settingsState.setPlayerId(settingsComponent.getPlayerId());
settingsState.setEditionId(settingsComponent.getEdition().getIdentifier());
settingsState.setVolume(settingsComponent.getVolume());
// 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 int intervalTimeBetweenNotifications; // in minutes
private boolean autoPlayAudio;
private String playerId;
private String editionId;
private int volume;
public static AyahSettingsState getInstance() {
@ -36,9 +36,9 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
intervalTimeBetweenNotifications = 30; // 30 minutes
autoPlayAudio = false;
try {
playerId = Edition.getRandomEdition(EditionFormat.AUDIO, "ar").getIdentifier();
editionId = Edition.getRandomEdition(EditionFormat.AUDIO, "ar").getIdentifier();
} catch (final IOException e) {
playerId = null;
editionId = null;
}
volume = 40; // 40%
}
@ -78,12 +78,12 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
this.autoPlayAudio = autoPlayAudio;
}
public String getPlayerId() {
return playerId;
public String getEditionId() {
return editionId;
}
public void setPlayerId(final String playerId) {
this.playerId = playerId;
public void setEditionId(final String editionId) {
this.editionId = editionId;
}
public int getVolume() {

View file

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