Fix AudioPlayer issue 🔊 and remmove sound volume futare 🥲

This commit is contained in:
Anas Elgarhy 2022-08-21 10:26:46 +02:00
parent f1e1047ebc
commit 8859412756
9 changed files with 20 additions and 71 deletions

View file

@ -14,7 +14,8 @@ 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.github.goxr3plus:java-stream-player:10.0.2")
implementation("com.googlecode.soundlibs:jlayer:1.0.1.4")
} }
java { java {
sourceCompatibility = JavaVersion.VERSION_11 sourceCompatibility = JavaVersion.VERSION_11

View file

@ -29,7 +29,7 @@ public class AyahStartupActivity implements StartupActivity {
.createNotification(bassmalh.getText(), NotificationType.INFORMATION).notify(project); .createNotification(bassmalh.getText(), NotificationType.INFORMATION).notify(project);
if (basmalhOnStartSettingsState.isSoundActive()) { if (basmalhOnStartSettingsState.isSoundActive()) {
new AudioPlayer(basmalhOnStartSettingsState.getVolume(), bassmalh.getAudioUrl()).play(); new AudioPlayer(bassmalh.getAudioUrl()).play();
} }
} catch (final IOException e) { } catch (final IOException e) {
e.printStackTrace(); e.printStackTrace();

View file

@ -46,7 +46,7 @@ public class NotificationTimerTask extends TimerTask {
public void actionPerformed(@NotNull final AnActionEvent e) { public void actionPerformed(@NotNull final AnActionEvent e) {
LOGGER.info("Play action performed"); LOGGER.info("Play action performed");
LOGGER.info("Audio url: " + randomAyah.getAudioUrl()); LOGGER.info("Audio url: " + randomAyah.getAudioUrl());
play(settings.getVolume(), randomAyah.getAudioUrl()); play(randomAyah.getAudioUrl());
} }
}); });
@ -73,15 +73,15 @@ 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");
play(settings.getVolume(), randomAyah.getAudioUrl()); play(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) { private void play(final String audioUrl) {
new AudioPlayer(volume, audioUrl).play(); new AudioPlayer(audioUrl).play();
} }
public void setProject(final Project project) { public void setProject(final Project project) {

View file

@ -1,7 +1,8 @@
package com.anas.intellij.plugins.ayah.audio; package com.anas.intellij.plugins.ayah.audio;
import com.goxr3plus.streamplayer.stream.StreamPlayer; import javazoom.jl.decoder.JavaLayerException;
import com.goxr3plus.streamplayer.stream.StreamPlayerException; import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.Player;
import java.io.BufferedInputStream; import java.io.BufferedInputStream;
import java.io.IOException; import java.io.IOException;
@ -15,39 +16,34 @@ import java.util.logging.Logger;
* @date: 8/19/22 * @date: 8/19/22
*/ */
public class AudioPlayer { public class AudioPlayer {
private final StreamPlayer streamPlayer;
private final String audioUrl; private final String audioUrl;
private final float volume;
private static final Logger LOGGER = Logger.getLogger(AudioPlayer.class.getName()); private static final Logger LOGGER = Logger.getLogger(AudioPlayer.class.getName());
public AudioPlayer(final int volume, final String audioUrl) { public AudioPlayer(final String audioUrl) {
streamPlayer = new StreamPlayer();
this.audioUrl = audioUrl; this.audioUrl = audioUrl;
this.volume = volume / 100f;
} }
private void loadAndOpen() { private Player loadAndOpen() {
try { try {
streamPlayer.open(getInputStream(audioUrl)); return new Player(getInputStream(audioUrl),
} catch (final MalformedURLException | StreamPlayerException e) { FactoryRegistry.systemRegistry().createAudioDevice());
} catch (final MalformedURLException | JavaLayerException e) {
LOGGER.severe("Error while opening stream player: " + e.getMessage()); LOGGER.severe("Error while opening stream player: " + e.getMessage());
} catch (final IOException e) { } catch (final IOException e) {
LOGGER.severe("Can't load audio file: " + audioUrl); LOGGER.severe("Can't load audio file: " + audioUrl);
LOGGER.severe(e.getMessage()); LOGGER.severe(e.getMessage());
} }
return null;
} }
public void play() { public void play() {
new Thread(() -> { new Thread(() -> {
try { try {
loadAndOpen(); loadAndOpen().play();
streamPlayer.play(); } catch (final JavaLayerException | NullPointerException e) {
} catch (final StreamPlayerException e) {
LOGGER.severe(e.getMessage()); LOGGER.severe(e.getMessage());
} }
}).start(); }).start();
streamPlayer.setGain(volume);
} }

View file

@ -36,7 +36,7 @@ public class AyahDetailsDialog extends JDialog {
private void addListeners(final Ayah ayah) { private void addListeners(final Ayah ayah) {
playButton.addActionListener(e -> playButton.addActionListener(e ->
new AudioPlayer(AyahSettingsState.getInstance().getVolume(), ayah.getAudioUrl()).play()); new AudioPlayer(ayah.getAudioUrl()).play());
buttonCancel.addActionListener(l -> dispose()); buttonCancel.addActionListener(l -> dispose());

View file

@ -39,7 +39,6 @@ public class AyahSettingsConfigurable implements Configurable {
settingsState.setIntervalTimeBetweenNotifications(settingsComponent.getIntervalTimeBetweenNotifications()); settingsState.setIntervalTimeBetweenNotifications(settingsComponent.getIntervalTimeBetweenNotifications());
settingsState.setAutoPlayAudio(settingsComponent.isAutoPlayAudio()); settingsState.setAutoPlayAudio(settingsComponent.isAutoPlayAudio());
settingsState.setEditionId(settingsComponent.getEdition().getIdentifier()); 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 // Update the timer with the new interval time between notifications if interval time between notifications has changed
if (settingsState.getIntervalTimeBetweenNotifications() != if (settingsState.getIntervalTimeBetweenNotifications() !=

View file

@ -25,7 +25,6 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
private int intervalTimeBetweenNotifications; // in minutes private int intervalTimeBetweenNotifications; // in minutes
private boolean autoPlayAudio; private boolean autoPlayAudio;
private String editionId; private String editionId;
private int volume;
public static AyahSettingsState getInstance() { public static AyahSettingsState getInstance() {
return ApplicationManager.getApplication().getService(AyahSettingsState.class); return ApplicationManager.getApplication().getService(AyahSettingsState.class);
@ -40,7 +39,6 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
} catch (final IOException e) { } catch (final IOException e) {
editionId = null; editionId = null;
} }
volume = 40; // 40%
} }
@ -85,12 +83,4 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
public void setEditionId(final String editionId) { public void setEditionId(final String editionId) {
this.editionId = editionId; this.editionId = editionId;
} }
public int getVolume() {
return volume;
}
public void setVolume(final int volume) {
this.volume = volume;
}
} }

View file

@ -14,7 +14,6 @@ public class BasmalhOnStart {
private boolean isNotificationActive; private boolean isNotificationActive;
private boolean isSoundActive; private boolean isSoundActive;
private String editionId; private String editionId;
private int volume;
public BasmalhOnStart() { public BasmalhOnStart() {
isActive = true; isActive = true;
@ -25,7 +24,6 @@ public class BasmalhOnStart {
} catch (final IOException e) { } catch (final IOException e) {
editionId = null; editionId = null;
} }
volume = 40; // 40%
} }
public boolean isActive() { public boolean isActive() {
@ -59,12 +57,4 @@ public class BasmalhOnStart {
public void setEditionId(final String editionId) { public void setEditionId(final String editionId) {
this.editionId = editionId; this.editionId = editionId;
} }
public int getVolume() {
return volume;
}
public void setVolume(final int volume) {
this.volume = volume;
}
} }

View file

@ -25,12 +25,10 @@ public class SettingsComponent {
private final JBCheckBox basmalhOnStartCheckBox; private final JBCheckBox basmalhOnStartCheckBox;
private final JBCheckBox autoPlayBasmalhCheckBox; private final JBCheckBox autoPlayBasmalhCheckBox;
private final JComboBox<ReadableEdition> basmalhPlayerIdComboBox; private final JComboBox<ReadableEdition> basmalhPlayerIdComboBox;
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<ReadableEdition> ayahPlayerIdComboBox; private final JComboBox<ReadableEdition> ayahPlayerIdComboBox;
private final JBSlider ayahVolumeSlider;
private final Logger LOGGER = Logger.getLogger(SettingsComponent.class.getName()); private final Logger LOGGER = Logger.getLogger(SettingsComponent.class.getName());
@ -39,11 +37,9 @@ public class SettingsComponent {
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");
basmalhPlayerIdComboBox = new JComboBox<>(); basmalhPlayerIdComboBox = new JComboBox<>();
basmalhVolumeSlider = new JBSlider();
notificationsIntervalSpinner = new JSpinner(); notificationsIntervalSpinner = new JSpinner();
notificationsAudioCheckBox = new JBCheckBox("Notifications audio"); notificationsAudioCheckBox = new JBCheckBox("Notifications audio");
ayahPlayerIdComboBox = new JComboBox<>(); ayahPlayerIdComboBox = new JComboBox<>();
ayahVolumeSlider = new JBSlider();
} }
public SettingsComponent() { public SettingsComponent() {
@ -60,9 +56,6 @@ public class SettingsComponent {
"gap unrelated") "gap unrelated")
.addComponent(basmalhPlayerIdComboBox, .addComponent(basmalhPlayerIdComboBox,
"grow, wrap") "grow, wrap")
.addComponent(new JBLabel("Volume"),
"gap 1")
.addComponent(basmalhVolumeSlider, "grow, wrap")
.build(), .build(),
"span, grow, wrap" "span, grow, wrap"
) )
@ -73,8 +66,6 @@ public class SettingsComponent {
.addComponent(notificationsAudioCheckBox, "grow") .addComponent(notificationsAudioCheckBox, "grow")
.addComponent(new JBLabel("Ayah player"), "gap unrelated") .addComponent(new JBLabel("Ayah player"), "gap unrelated")
.addComponent(ayahPlayerIdComboBox, "grow, wrap") .addComponent(ayahPlayerIdComboBox, "grow, wrap")
.addComponent(new JBLabel("Volume"), "gap unrelated")
.addComponent(ayahVolumeSlider, "grow, wrap")
.build() .build()
) )
.getPanel(); .getPanel();
@ -96,17 +87,6 @@ public class SettingsComponent {
notificationsAudioCheckBox.setSelected(settings.isAutoPlayAudio()); notificationsAudioCheckBox.setSelected(settings.isAutoPlayAudio());
basmalhPlayerIdComboBox.setEnabled(settings.getBasmalhOnStart().isActive()); basmalhPlayerIdComboBox.setEnabled(settings.getBasmalhOnStart().isActive());
basmalhVolumeSlider.setValue(settings.getBasmalhOnStart().getVolume());
basmalhVolumeSlider.setPaintLabels(true);
basmalhVolumeSlider.setPaintTicks(true);
basmalhVolumeSlider.setMajorTickSpacing(20);
basmalhVolumeSlider.setMinorTickSpacing(10);
ayahVolumeSlider.setValue(settings.getVolume());
ayahVolumeSlider.setPaintLabels(true);
ayahVolumeSlider.setPaintTicks(true);
ayahVolumeSlider.setMajorTickSpacing(20);
ayahVolumeSlider.setMinorTickSpacing(10);
if (settings.getBasmalhOnStart().getEditionId() != null) { if (settings.getBasmalhOnStart().getEditionId() != null) {
basmalhPlayerIdComboBox.setSelectedItem(new ReadableEdition(settings.getBasmalhOnStart().getEditionId())); basmalhPlayerIdComboBox.setSelectedItem(new ReadableEdition(settings.getBasmalhOnStart().getEditionId()));
} }
@ -149,9 +129,7 @@ public class SettingsComponent {
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() ||
settings.isAutoPlayAudio() != notificationsAudioCheckBox.isSelected() || settings.isAutoPlayAudio() != notificationsAudioCheckBox.isSelected();
settings.getBasmalhOnStart().getVolume() != basmalhVolumeSlider.getValue() ||
settings.getVolume() != ayahVolumeSlider.getValue();
} }
public void reset() { public void reset() {
@ -168,7 +146,6 @@ public class SettingsComponent {
b.setSoundActive(autoPlayBasmalhCheckBox.isSelected()); b.setSoundActive(autoPlayBasmalhCheckBox.isSelected());
b.setEditionId(((ReadableEdition) Objects.requireNonNull( b.setEditionId(((ReadableEdition) Objects.requireNonNull(
basmalhPlayerIdComboBox.getSelectedItem())).getEdition().getIdentifier()); basmalhPlayerIdComboBox.getSelectedItem())).getEdition().getIdentifier());
b.setVolume(basmalhVolumeSlider.getValue());
return b; return b;
} }
@ -183,8 +160,4 @@ public class SettingsComponent {
public Edition getEdition() { public Edition getEdition() {
return ((ReadableEdition) ayahPlayerIdComboBox.getSelectedItem()).getEdition(); return ((ReadableEdition) ayahPlayerIdComboBox.getSelectedItem()).getEdition();
} }
public int getVolume() {
return ayahVolumeSlider.getValue();
}
} }