2828import xyz .gianlu .librespot .player .codecs .Codec ;
2929import xyz .gianlu .librespot .player .contexts .AbsSpotifyContext ;
3030
31- import java .io .*;
31+ import java .io .Closeable ;
32+ import java .io .File ;
33+ import java .io .FileOutputStream ;
34+ import java .io .IOException ;
3235import java .nio .charset .StandardCharsets ;
3336import java .util .ArrayList ;
3437import java .util .Base64 ;
@@ -781,12 +784,11 @@ private static class MetadataPipe {
781784 private static final String CODE_PVOL = "70766f6c" ;
782785 private static final String CODE_PRGR = "70726772" ;
783786 private static final String CODE_PICT = "50494354" ;
784- private final FileOutputStream out ;
787+ private final File file ;
788+ private FileOutputStream out ;
785789
786- MetadataPipe (@ NotNull Configuration conf ) throws FileNotFoundException {
787- File file = conf .metadataPipe ();
788- if (file != null ) out = new FileOutputStream (file );
789- else out = null ;
790+ MetadataPipe (@ NotNull Configuration conf ) {
791+ file = conf .metadataPipe ();
790792 }
791793
792794 void safeSend (@ NotNull String type , @ NotNull String code , @ Nullable String payload ) {
@@ -805,8 +807,9 @@ void safeSend(@NotNull String type, @NotNull String code, @Nullable byte[] paylo
805807 }
806808 }
807809
808- private void send (@ NotNull String type , @ NotNull String code , @ Nullable byte [] payload ) throws IOException {
809- if (out == null ) return ;
810+ private synchronized void send (@ NotNull String type , @ NotNull String code , @ Nullable byte [] payload ) throws IOException {
811+ if (file == null ) return ;
812+ if (out == null ) out = new FileOutputStream (file );
810813
811814 if (payload != null ) {
812815 out .write (String .format ("<item><type>%s</type><code>%s</code><length>%d</length>\n <data encoding=\" base64\" >%s</data></item>\n " , type , code ,
@@ -817,7 +820,7 @@ private void send(@NotNull String type, @NotNull String code, @Nullable byte[] p
817820 }
818821
819822 boolean enabled () {
820- return out != null ;
823+ return file != null ;
821824 }
822825 }
823826
@@ -827,11 +830,7 @@ private class EventsDispatcher {
827830 private final List <EventsListener > listeners = new ArrayList <>();
828831
829832 EventsDispatcher (@ NotNull Configuration conf ) {
830- try {
831- metadataPipe = new MetadataPipe (conf );
832- } catch (FileNotFoundException ex ) {
833- throw new IllegalArgumentException (ex );
834- }
833+ metadataPipe = new MetadataPipe (conf );
835834 }
836835
837836 private void sendImage () {
@@ -872,6 +871,29 @@ private void sendProgress() {
872871 metadataPipe .safeSend (MetadataPipe .TYPE_SSNC , MetadataPipe .CODE_PRGR , data );
873872 }
874873
874+ private void sendTrackInfo () {
875+ Metadata .Track track = currentTrack ();
876+ Metadata .Episode episode = currentEpisode ();
877+ if (track == null && episode == null ) return ;
878+
879+ String title = track != null ? track .getName () : episode .getName ();
880+ metadataPipe .safeSend (MetadataPipe .TYPE_CORE , MetadataPipe .CODE_MINM , title );
881+
882+ String album = track != null ? track .getAlbum ().getName () : episode .getShow ().getName ();
883+ metadataPipe .safeSend (MetadataPipe .TYPE_CORE , MetadataPipe .CODE_ASAL , album );
884+
885+ String artist = track != null ? Utils .artistsToString (track .getArtistList ()) : episode .getShow ().getPublisher ();
886+ metadataPipe .safeSend (MetadataPipe .TYPE_CORE , MetadataPipe .CODE_ASAR , artist );
887+ }
888+
889+ private void sendVolume (int value ) {
890+ float xmlValue ;
891+ if (value == 0 ) xmlValue = 144.0f ;
892+ else xmlValue = (value - PlayerRunner .VOLUME_MAX ) * 30.0f / (PlayerRunner .VOLUME_MAX - 1 );
893+ String volData = String .format ("%.2f,0.00,0.00,0.00" , xmlValue );
894+ metadataPipe .safeSend (MetadataPipe .TYPE_SSNC , MetadataPipe .CODE_PVOL , volData );
895+ }
896+
875897 void playbackPaused () {
876898 long trackTime = state .getPosition ();
877899 for (EventsListener l : new ArrayList <>(listeners ))
@@ -914,7 +936,7 @@ void seeked(int pos) {
914936 for (EventsListener l : new ArrayList <>(listeners ))
915937 executorService .execute (() -> l .onTrackSeeked (pos ));
916938
917- if (metadataPipe .enabled ()) sendProgress ( );
939+ if (metadataPipe .enabled ()) executorService . execute ( this :: sendProgress );
918940 }
919941
920942 void volumeChanged (@ Range (from = 0 , to = PlayerRunner .VOLUME_MAX ) int value ) {
@@ -923,13 +945,7 @@ void volumeChanged(@Range(from = 0, to = PlayerRunner.VOLUME_MAX) int value) {
923945 for (EventsListener l : new ArrayList <>(listeners ))
924946 executorService .execute (() -> l .onVolumeChanged (volume ));
925947
926- if (metadataPipe .enabled ()) {
927- float xmlValue ;
928- if (value == 0 ) xmlValue = 144.0f ;
929- else xmlValue = (value - PlayerRunner .VOLUME_MAX ) * 30.0f / (PlayerRunner .VOLUME_MAX - 1 );
930- String volData = String .format ("%.2f,0.00,0.00,0.00" , xmlValue );
931- metadataPipe .safeSend (MetadataPipe .TYPE_SSNC , MetadataPipe .CODE_PVOL , volData );
932- }
948+ if (metadataPipe .enabled ()) executorService .execute (() -> sendVolume (value ));
933949 }
934950
935951 void metadataAvailable () {
@@ -943,17 +959,11 @@ void metadataAvailable() {
943959 executorService .execute (() -> l .onMetadataAvailable (track , episode ));
944960
945961 if (metadataPipe .enabled ()) {
946- String title = track != null ? track .getName () : episode .getName ();
947- metadataPipe .safeSend (MetadataPipe .TYPE_CORE , MetadataPipe .CODE_MINM , title );
948-
949- String album = track != null ? track .getAlbum ().getName () : episode .getShow ().getName ();
950- metadataPipe .safeSend (MetadataPipe .TYPE_CORE , MetadataPipe .CODE_ASAL , album );
951-
952- String artist = track != null ? Utils .artistsToString (track .getArtistList ()) : episode .getShow ().getPublisher ();
953- metadataPipe .safeSend (MetadataPipe .TYPE_CORE , MetadataPipe .CODE_ASAR , artist );
954-
955- sendProgress ();
956- sendImage ();
962+ executorService .execute (() -> {
963+ sendTrackInfo ();
964+ sendProgress ();
965+ sendImage ();
966+ });
957967 }
958968 }
959969
0 commit comments