mirror of
https://github.com/anas-elgarhy/Ayah-intellij.git
synced 2024-08-15 00:43:43 +00:00
Merge pull request #7 from anas-elgarhy/improve-details-dialog
Add next and previous buttons and auto play check box 🥰
This commit is contained in:
commit
a9803f31ff
4 changed files with 219 additions and 35 deletions
|
@ -2,8 +2,11 @@ package com.anas.intellij.plugins.ayah.audio;
|
||||||
|
|
||||||
import javazoom.jl.decoder.JavaLayerException;
|
import javazoom.jl.decoder.JavaLayerException;
|
||||||
import javazoom.jl.player.FactoryRegistry;
|
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.BufferedInputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
@ -17,21 +20,27 @@ import java.util.logging.Logger;
|
||||||
*/
|
*/
|
||||||
public class AudioPlayer {
|
public class AudioPlayer {
|
||||||
private final String audioUrl;
|
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());
|
private static final Logger LOGGER = Logger.getLogger(AudioPlayer.class.getName());
|
||||||
|
|
||||||
public AudioPlayer(final String audioUrl) {
|
public AudioPlayer(final String audioUrl) {
|
||||||
this.audioUrl = audioUrl;
|
this.audioUrl = audioUrl;
|
||||||
}
|
}
|
||||||
|
|
||||||
private Player loadAndOpen() {
|
private AdvancedPlayer loadAndOpen() {
|
||||||
try {
|
try {
|
||||||
return new Player(getInputStream(audioUrl),
|
return new AdvancedPlayer(getInputStream(audioUrl),
|
||||||
FactoryRegistry.systemRegistry().createAudioDevice());
|
FactoryRegistry.systemRegistry().createAudioDevice());
|
||||||
} catch (final MalformedURLException | JavaLayerException e) {
|
} 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());
|
||||||
|
|
||||||
|
JOptionPane.showMessageDialog(null,
|
||||||
|
"Error loading the ayah, check your internet connection - حدث خطاء اثناء تحميل الآية، تحقق من اتصالك بالإنترنت",
|
||||||
|
"Error - خطأ", JOptionPane.ERROR_MESSAGE);
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -39,9 +48,31 @@ public class AudioPlayer {
|
||||||
public void play() {
|
public void play() {
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
try {
|
try {
|
||||||
loadAndOpen().play();
|
player = loadAndOpen();
|
||||||
} catch (final JavaLayerException | NullPointerException e) {
|
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());
|
LOGGER.severe(e.getMessage());
|
||||||
|
|
||||||
|
JOptionPane.showMessageDialog(null,
|
||||||
|
"Error whole playing the ayah - حدث خطاء اثناء تشغيل الآية",
|
||||||
|
"Error - خطأ", JOptionPane.ERROR_MESSAGE);
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
@ -52,4 +83,15 @@ public class AudioPlayer {
|
||||||
final var inputStream = url.openStream();
|
final var inputStream = url.openStream();
|
||||||
return new BufferedInputStream(inputStream);
|
return new BufferedInputStream(inputStream);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public AudioPlayer setListener(final PlayerListener listener) {
|
||||||
|
this.listener = listener;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
if (player != null) {
|
||||||
|
player.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
|
@ -10,7 +10,7 @@
|
||||||
</properties>
|
</properties>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
<children>
|
<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"/>
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
<constraints>
|
<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"/>
|
<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/>
|
<properties/>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
<children>
|
<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"/>
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties/>
|
<properties/>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
<children>
|
<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">
|
<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"/>
|
<margin top="0" left="0" bottom="0" right="0"/>
|
||||||
<constraints>
|
<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>
|
</constraints>
|
||||||
<properties/>
|
<properties/>
|
||||||
<border type="none"/>
|
<border type="none"/>
|
||||||
|
@ -76,8 +60,51 @@
|
||||||
</component>
|
</component>
|
||||||
</children>
|
</children>
|
||||||
</grid>
|
</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>
|
</children>
|
||||||
</grid>
|
</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>
|
</children>
|
||||||
</grid>
|
</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">
|
<grid id="e3588" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
package com.anas.intellij.plugins.ayah.dialogs;
|
package com.anas.intellij.plugins.ayah.dialogs;
|
||||||
|
|
||||||
import com.anas.alqurancloudapi.Ayah;
|
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.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 javax.swing.*;
|
||||||
import java.awt.event.KeyEvent;
|
import java.awt.event.KeyEvent;
|
||||||
import java.awt.event.WindowAdapter;
|
import java.awt.event.WindowAdapter;
|
||||||
import java.awt.event.WindowEvent;
|
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 JPanel contentPane;
|
||||||
private JButton playButton;
|
private JButton playButton;
|
||||||
private JButton buttonCancel;
|
private JButton buttonCancel;
|
||||||
|
@ -17,8 +20,16 @@ public class AyahDetailsDialog extends JDialog {
|
||||||
private JLabel surahNameLabel;
|
private JLabel surahNameLabel;
|
||||||
private JLabel numberOfAyahInSuarhLabel;
|
private JLabel numberOfAyahInSuarhLabel;
|
||||||
private JLabel ayahRevelationType;
|
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) {
|
public AyahDetailsDialog(final Ayah ayah) {
|
||||||
|
this.ayah = ayah;
|
||||||
|
|
||||||
setContentPane(contentPane);
|
setContentPane(contentPane);
|
||||||
setModal(true);
|
setModal(true);
|
||||||
setSize(500, 300);
|
setSize(500, 300);
|
||||||
|
@ -26,31 +37,123 @@ public class AyahDetailsDialog extends JDialog {
|
||||||
setLocationRelativeTo(null);
|
setLocationRelativeTo(null);
|
||||||
getRootPane().setDefaultButton(playButton);
|
getRootPane().setDefaultButton(playButton);
|
||||||
|
|
||||||
|
updateAhaDetails();
|
||||||
|
|
||||||
|
addListeners();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateAhaDetails() {
|
||||||
ayahTextArea.setText(ayah.getText());
|
ayahTextArea.setText(ayah.getText());
|
||||||
surahNameLabel.setText(ayah.getSurah().getName());
|
surahNameLabel.setText(ayah.getSurah().getName());
|
||||||
numberOfAyahInSuarhLabel.setText("آية رقم: " + ayah.getNumberInSurah());
|
numberOfAyahInSuarhLabel.setText("آية رقم: " + ayah.getNumberInSurah());
|
||||||
ayahRevelationType.setText(ayah.getSurah().getRevelationType().getArabicName());
|
ayahRevelationType.setText(ayah.getSurah().getRevelationType().getArabicName());
|
||||||
|
|
||||||
addListeners(ayah);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addListeners(final Ayah ayah) {
|
private void addListeners() {
|
||||||
playButton.addActionListener(e ->
|
playButton.addActionListener(e -> {
|
||||||
new AudioPlayer(ayah.getAudioUrl()).play());
|
if (!isPlaying) {
|
||||||
|
audioPlayer = new AudioPlayer(ayah.getAudioUrl()).setListener(this);
|
||||||
|
audioPlayer.play();
|
||||||
|
} else {
|
||||||
|
audioPlayer.stop();
|
||||||
|
playButton.setText("Play");
|
||||||
|
isPlaying = false;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
buttonCancel.addActionListener(l -> dispose());
|
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 -> close());
|
||||||
|
|
||||||
// call onCancel() when cross is clicked
|
// call onCancel() when cross is clicked
|
||||||
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
|
||||||
addWindowListener(new WindowAdapter() {
|
addWindowListener(new WindowAdapter() {
|
||||||
public void windowClosing(WindowEvent e) {
|
public void windowClosing(WindowEvent e) {
|
||||||
dispose();
|
close();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// call onCancel() on ESCAPE
|
// call onCancel() on ESCAPE
|
||||||
contentPane.registerKeyboardAction(l -> dispose(),
|
contentPane.registerKeyboardAction(l -> close(),
|
||||||
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
|
KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0),
|
||||||
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void close() {
|
||||||
|
if (isPlaying) {
|
||||||
|
audioPlayer.stop();
|
||||||
|
audioPlayer = null;
|
||||||
|
}
|
||||||
|
dispose();
|
||||||
|
}
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue