Skip to content

Commit

Permalink
feature: add player status line
Browse files Browse the repository at this point in the history
  • Loading branch information
mirkoebert committed Nov 21, 2024
1 parent 2c48791 commit 904603e
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,13 @@
<url>https://jitpack.io</url>
</repository>
</repositories>

<distributionManagement>
<repository>
<id>github</id>
<name>mirkoebert</name>
<url>https://maven.pkg.github.com/mirkoebert/SimpleJavaRadioPlayerForSomaFM</url>
</repository>
</distributionManagement>

</project>
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mirkoebert.simplejavaradioplayer;

import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -25,6 +26,7 @@ public class Application extends JFrame {

private final StationService stationService = new StationService();
private final String nameAndVersion;
private final StatusLine statusLine = new StatusLine();
@Autowired
private PlayerService playerService;
private JDialog donationBox;
Expand Down Expand Up @@ -104,6 +106,8 @@ private void createLayout(final JComponent main, final JButton... buttons) {
main.setPreferredSize(new Dimension(350, 400));
pane.add(lp, BorderLayout.CENTER);

pane.add(statusLine, BorderLayout.SOUTH);

val pane1 = new JOptionPane("Please support Soma FM and go to https://somafm.com/support/", INFORMATION_MESSAGE);
donationBox = pane1.createDialog(pane, "Donate");

Expand All @@ -127,4 +131,8 @@ private void openSomaFmDonateLinkInDefaultBrowser() {
}
}

@PostConstruct
void registerObserver() {
playerService.addObserver(statusLine);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import org.springframework.stereotype.Service;

import java.net.URL;
import java.util.Observable;
import java.util.prefs.Preferences;

@Service
@RequiredArgsConstructor
@Slf4j
public class PlayerService {
public class PlayerService extends Observable {

private final ResilientStreamPlayer player;
private final StationService stationService;
Expand All @@ -26,10 +27,12 @@ void playButtonClicked() {
switch (playerStatus) {
case PLAYING -> {
log.info("Stop");
setStatus("Player stoped");
player.stop();
}
case NOT_SPECIFIED, STOPPED -> {
log.info("Start last station");
setStatus("Player started with last station: " + stationService.getStationsNames()[stationService.getSelectedStationIndex()]);
URL currenStationUrl = stationService.getSelectedStationPlsUrl();
log.info("Start station {}", currenStationUrl);
final URL stream = playListService.getAudioStreamURL(currenStationUrl);
Expand All @@ -41,6 +44,7 @@ void playButtonClicked() {

void listItemSelected(int selectedIndex) {
log.info("listItemSelected {}", selectedIndex);
setStatus("Play station: " + stationService.getStationsNames()[selectedIndex]);
stationService.setSelectedStationIndex(selectedIndex);
final URL nextStationUrl = stationService.getSelectedStationPlsUrl();
final Status playerStatus = player.getStatus();
Expand All @@ -56,7 +60,7 @@ void listItemSelected(int selectedIndex) {
URL stream = playListService.getAudioStreamURL(nextStationUrl);
player.playStream(stream);
}
default -> log.warn("unsopprted operation for player status {}", playerStatus);
default -> log.warn("unsupported operation for player status {}", playerStatus);
}

}
Expand All @@ -74,4 +78,9 @@ public void shutDown() {
log.info("See You Space Cowboy");
player.stop();
}

public void setStatus(String status) {
setChanged();
notifyObservers(status);
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/mirkoebert/simplejavaradioplayer/StatusLine.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.mirkoebert.simplejavaradioplayer;

import lombok.extern.slf4j.Slf4j;

import javax.swing.*;
import java.util.Observable;
import java.util.Observer;

@Slf4j
public class StatusLine extends JLabel implements Observer {


public StatusLine() {
super("Ready to play your favorite music.", SwingConstants.CENTER);
}

@Override
public void update(Observable o, Object arg) {
log.info("Set status: {}", arg);
try {
setText((String) arg);
} catch (Exception e) {
log.warn("Can't set status {}", arg, e);
}
}
}

0 comments on commit 904603e

Please sign in to comment.