🤍 Add next and previuos ayh button and auto play checkbox 🥰

This commit is contained in:
Anas Elgarhy 2022-08-22 14:05:51 +02:00
parent 22503baaf4
commit d983c475a2
4 changed files with 208 additions and 32 deletions

View file

@ -2,8 +2,11 @@ package com.anas.intellij.plugins.ayah.audio;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.FactoryRegistry;
import javazoom.jl.player.Player;
import javazoom.jl.player.advanced.AdvancedPlayer;
import javazoom.jl.player.advanced.PlaybackEvent;
import javazoom.jl.player.advanced.PlaybackListener;
import javax.swing.*;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
@ -17,21 +20,27 @@ import java.util.logging.Logger;
*/
public class AudioPlayer {
private final String audioUrl;
private PlayerListener listener; // we don't need more than one listener
private AdvancedPlayer player;
private static final Logger LOGGER = Logger.getLogger(AudioPlayer.class.getName());
public AudioPlayer(final String audioUrl) {
this.audioUrl = audioUrl;
}
private Player loadAndOpen() {
private AdvancedPlayer loadAndOpen() {
try {
return new Player(getInputStream(audioUrl),
return new AdvancedPlayer(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());
JOptionPane.showMessageDialog(null,
"Error loading the ayah, check your internet connection - حدث خطاء اثناء تحميل الآية، تحقق من اتصالك بالإنترنت",
"Error - خطأ", JOptionPane.ERROR_MESSAGE);
}
return null;
}
@ -39,9 +48,31 @@ public class AudioPlayer {
public void play() {
new Thread(() -> {
try {
loadAndOpen().play();
} catch (final JavaLayerException | NullPointerException e) {
player = loadAndOpen();
if (player == null)
throw new IOException("Can't create player");
if (listener != null) {
player.setPlayBackListener(new PlaybackListener() {
@Override
public void playbackStarted(final PlaybackEvent evt) {
listener.onStarted(evt);
}
@Override
public void playbackFinished(final PlaybackEvent evt) {
listener.onFinished(evt);
}
});
}
player.play();
} catch (final JavaLayerException | IOException e) {
LOGGER.severe(e.getMessage());
JOptionPane.showMessageDialog(null,
"Error whole playing the ayah - حدث خطاء اثناء تشغيل الآية",
"Error - خطأ", JOptionPane.ERROR_MESSAGE);
}
}).start();
}
@ -52,4 +83,15 @@ public class AudioPlayer {
final var inputStream = url.openStream();
return new BufferedInputStream(inputStream);
}
public AudioPlayer setListener(final PlayerListener listener) {
this.listener = listener;
return this;
}
public void stop() {
if (player != null) {
player.close();
}
}
}

View file

@ -0,0 +1,12 @@
package com.anas.intellij.plugins.ayah.audio;
import javazoom.jl.player.advanced.PlaybackEvent;
/**
* @author: <a href="https://github.com/anas-elgarhy">Anas Elgarhy</a>
* @date: 8/22/22
*/
public interface PlayerListener {
void onStarted(final PlaybackEvent event);
void onFinished(final PlaybackEvent event);
}

View file

@ -10,7 +10,7 @@
</properties>
<border type="none"/>
<children>
<grid id="94766" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="94766" layout-manager="GridLayoutManager" row-count="2" column-count="6" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="1" column="0" row-span="1" col-span="3" vsize-policy="1" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
@ -18,34 +18,18 @@
<properties/>
<border type="none"/>
<children>
<grid id="9538f" layout-manager="GridLayoutManager" row-count="2" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="9538f" layout-manager="GridLayoutManager" row-count="2" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="6" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="e7465" class="javax.swing.JButton" binding="playButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Play"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Cancel"/>
</properties>
</component>
<grid id="c8e9d" layout-manager="GridLayoutManager" row-count="1" column-count="3" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="4" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
@ -76,8 +60,51 @@
</component>
</children>
</grid>
<component id="69418" class="javax.swing.JButton" binding="previousButton">
<constraints>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Previous"/>
<toolTipText value="Previous Aah"/>
</properties>
</component>
<component id="b465c" class="javax.swing.JButton" binding="nextButton" default-binding="true">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<hideActionText value="true"/>
<text value="Next"/>
<toolTipText value="Nexit ayah"/>
</properties>
</component>
</children>
</grid>
<component id="893ab" class="javax.swing.JCheckBox" binding="autoPlayCheckBox" default-binding="true">
<constraints>
<grid row="1" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Auto play"/>
</properties>
</component>
<component id="5723f" class="javax.swing.JButton" binding="buttonCancel">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Cancel"/>
</properties>
</component>
<component id="e7465" class="javax.swing.JButton" binding="playButton">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Play"/>
</properties>
</component>
</children>
</grid>
<grid id="e3588" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">

View file

@ -1,15 +1,18 @@
package com.anas.intellij.plugins.ayah.dialogs;
import com.anas.alqurancloudapi.Ayah;
import com.anas.alqurancloudapi.consts.Constants;
import com.anas.intellij.plugins.ayah.audio.AudioPlayer;
import com.anas.intellij.plugins.ayah.settings.AyahSettingsState;
import com.anas.intellij.plugins.ayah.audio.PlayerListener;
import javazoom.jl.player.advanced.PlaybackEvent;
import javax.swing.*;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
public class AyahDetailsDialog extends JDialog {
public class AyahDetailsDialog extends JDialog implements PlayerListener {
private JPanel contentPane;
private JButton playButton;
private JButton buttonCancel;
@ -17,8 +20,16 @@ public class AyahDetailsDialog extends JDialog {
private JLabel surahNameLabel;
private JLabel numberOfAyahInSuarhLabel;
private JLabel ayahRevelationType;
private JButton previousButton;
private JButton nextButton;
private JCheckBox autoPlayCheckBox;
private boolean isPlaying;
private AudioPlayer audioPlayer;
private Ayah ayah;
public AyahDetailsDialog(final Ayah ayah) {
this.ayah = ayah;
setContentPane(contentPane);
setModal(true);
setSize(500, 300);
@ -26,17 +37,55 @@ public class AyahDetailsDialog extends JDialog {
setLocationRelativeTo(null);
getRootPane().setDefaultButton(playButton);
updateAhaDetails();
addListeners();
}
private void updateAhaDetails() {
ayahTextArea.setText(ayah.getText());
surahNameLabel.setText(ayah.getSurah().getName());
numberOfAyahInSuarhLabel.setText("آية رقم: " + ayah.getNumberInSurah());
ayahRevelationType.setText(ayah.getSurah().getRevelationType().getArabicName());
addListeners(ayah);
}
private void addListeners(final Ayah ayah) {
playButton.addActionListener(e ->
new AudioPlayer(ayah.getAudioUrl()).play());
private void addListeners() {
playButton.addActionListener(e -> {
if (!isPlaying) {
audioPlayer = new AudioPlayer(ayah.getAudioUrl()).setListener(this);
audioPlayer.play();
} else {
audioPlayer.stop();
playButton.setText("Play");
isPlaying = false;
}
});
nextButton.addActionListener(e -> {
if (ayah.getNumber() <= Constants.AYAHS_COUNT) {
loadTheAyah(ayah.getNumber() + 1);
previousButton.setEnabled(true);
if (ayah.getNumber() >= Constants.AYAHS_COUNT) {
nextButton.setEnabled(false);
}
if (isPlaying) {
playOrNot();
}
}
});
previousButton.addActionListener(e -> {
if (ayah.getNumber() >= 1) {
loadTheAyah(ayah.getNumber() - 1);
nextButton.setEnabled(true);
if (ayah.getNumber() == 1) {
previousButton.setEnabled(false);
}
if (isPlaying) {
playOrNot();
}
}
});
buttonCancel.addActionListener(l -> dispose());
@ -53,4 +102,50 @@ public class AyahDetailsDialog extends JDialog {
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
}
private void playOrNot() {
if (autoPlayCheckBox.isSelected()) {
audioPlayer.stop();
audioPlayer = new AudioPlayer(ayah.getAudioUrl()).setListener(this);
audioPlayer.play();
} else {
audioPlayer.stop();
playButton.setText("Play");
isPlaying = false;
}
}
private boolean loadTheAyah(final int ayhNumber) {
try {
ayah = Ayah.getAyah(ayhNumber,
ayah.getEdition().getIdentifier());
updateAhaDetails();
return true;
} catch (final IOException ex) {
JOptionPane.showMessageDialog(this,
"Error loading the ayah, check your internet connection - حدث خطاء اثناء تحميل الآية، تحقق من اتصالك بالإنترنت",
"Error - خطأ", JOptionPane.ERROR_MESSAGE);
}
return false;
}
@Override
public void onStarted(final PlaybackEvent event) {
playButton.setText("Stop");
isPlaying = true;
}
@Override
public void onFinished(final PlaybackEvent event) {
if (autoPlayCheckBox.isSelected() && ayah.getNumber() <= Constants.AYAHS_COUNT) {
if (loadTheAyah(ayah.getNumber() + 1)) {
audioPlayer = new AudioPlayer(ayah.getAudioUrl()).setListener(this);
audioPlayer.stop();
audioPlayer.play();
}
} else {
playButton.setText("Play");
isPlaying = false;
}
}
}