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

View file

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

View file

@ -46,7 +46,7 @@ public class NotificationTimerTask extends TimerTask {
public void actionPerformed(@NotNull final AnActionEvent e) {
LOGGER.info("Play action performed");
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.
if (settings.isAutoPlayAudio()) {
LOGGER.info("Playing ayah");
play(settings.getVolume(), randomAyah.getAudioUrl());
play(randomAyah.getAudioUrl());
}
} catch (final IOException e) {
LOGGER.severe(e.getMessage());
}
}
private void play(final int volume, final String audioUrl) {
new AudioPlayer(volume, audioUrl).play();
private void play(final String audioUrl) {
new AudioPlayer(audioUrl).play();
}
public void setProject(final Project project) {

View file

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

View file

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

View file

@ -39,7 +39,6 @@ public class AyahSettingsConfigurable implements Configurable {
settingsState.setIntervalTimeBetweenNotifications(settingsComponent.getIntervalTimeBetweenNotifications());
settingsState.setAutoPlayAudio(settingsComponent.isAutoPlayAudio());
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
if (settingsState.getIntervalTimeBetweenNotifications() !=

View file

@ -25,7 +25,6 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
private int intervalTimeBetweenNotifications; // in minutes
private boolean autoPlayAudio;
private String editionId;
private int volume;
public static AyahSettingsState getInstance() {
return ApplicationManager.getApplication().getService(AyahSettingsState.class);
@ -40,7 +39,6 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
} catch (final IOException e) {
editionId = null;
}
volume = 40; // 40%
}
@ -85,12 +83,4 @@ public class AyahSettingsState implements PersistentStateComponent<AyahSettingsS
public void setEditionId(final String 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 isSoundActive;
private String editionId;
private int volume;
public BasmalhOnStart() {
isActive = true;
@ -25,7 +24,6 @@ public class BasmalhOnStart {
} catch (final IOException e) {
editionId = null;
}
volume = 40; // 40%
}
public boolean isActive() {
@ -59,12 +57,4 @@ public class BasmalhOnStart {
public void setEditionId(final String 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 autoPlayBasmalhCheckBox;
private final JComboBox<ReadableEdition> basmalhPlayerIdComboBox;
private final JBSlider basmalhVolumeSlider;
private final JSpinner notificationsIntervalSpinner;
private SpinnerNumberModel notificationsIntervalSpinnerModel;
private final JBCheckBox notificationsAudioCheckBox;
private final JComboBox<ReadableEdition> ayahPlayerIdComboBox;
private final JBSlider ayahVolumeSlider;
private final Logger LOGGER = Logger.getLogger(SettingsComponent.class.getName());
@ -39,11 +37,9 @@ public class SettingsComponent {
basmalhOnStartCheckBox = new JBCheckBox("Basmalh on start");
autoPlayBasmalhCheckBox = new JBCheckBox("Auto play basmalh audio");
basmalhPlayerIdComboBox = new JComboBox<>();
basmalhVolumeSlider = new JBSlider();
notificationsIntervalSpinner = new JSpinner();
notificationsAudioCheckBox = new JBCheckBox("Notifications audio");
ayahPlayerIdComboBox = new JComboBox<>();
ayahVolumeSlider = new JBSlider();
}
public SettingsComponent() {
@ -60,9 +56,6 @@ public class SettingsComponent {
"gap unrelated")
.addComponent(basmalhPlayerIdComboBox,
"grow, wrap")
.addComponent(new JBLabel("Volume"),
"gap 1")
.addComponent(basmalhVolumeSlider, "grow, wrap")
.build(),
"span, grow, wrap"
)
@ -73,8 +66,6 @@ public class SettingsComponent {
.addComponent(notificationsAudioCheckBox, "grow")
.addComponent(new JBLabel("Ayah player"), "gap unrelated")
.addComponent(ayahPlayerIdComboBox, "grow, wrap")
.addComponent(new JBLabel("Volume"), "gap unrelated")
.addComponent(ayahVolumeSlider, "grow, wrap")
.build()
)
.getPanel();
@ -96,17 +87,6 @@ public class SettingsComponent {
notificationsAudioCheckBox.setSelected(settings.isAutoPlayAudio());
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) {
basmalhPlayerIdComboBox.setSelectedItem(new ReadableEdition(settings.getBasmalhOnStart().getEditionId()));
}
@ -149,9 +129,7 @@ public class SettingsComponent {
settings.getIntervalTimeBetweenNotifications() != notificationsIntervalSpinnerModel.getNumber().intValue() ||
settings.getBasmalhOnStart().isActive() != basmalhOnStartCheckBox.isSelected() ||
settings.getBasmalhOnStart().isSoundActive() != autoPlayBasmalhCheckBox.isSelected() ||
settings.isAutoPlayAudio() != notificationsAudioCheckBox.isSelected() ||
settings.getBasmalhOnStart().getVolume() != basmalhVolumeSlider.getValue() ||
settings.getVolume() != ayahVolumeSlider.getValue();
settings.isAutoPlayAudio() != notificationsAudioCheckBox.isSelected();
}
public void reset() {
@ -168,7 +146,6 @@ public class SettingsComponent {
b.setSoundActive(autoPlayBasmalhCheckBox.isSelected());
b.setEditionId(((ReadableEdition) Objects.requireNonNull(
basmalhPlayerIdComboBox.getSelectedItem())).getEdition().getIdentifier());
b.setVolume(basmalhVolumeSlider.getValue());
return b;
}
@ -183,8 +160,4 @@ public class SettingsComponent {
public Edition getEdition() {
return ((ReadableEdition) ayahPlayerIdComboBox.getSelectedItem()).getEdition();
}
public int getVolume() {
return ayahVolumeSlider.getValue();
}
}