1010
1111package com .goxr3plus .streamplayer .stream ;
1212
13- import java .io .File ;
13+ import com .goxr3plus .streamplayer .enums .Status ;
14+ import com .goxr3plus .streamplayer .stream .StreamPlayerException .PlayerException ;
15+ import javazoom .spi .PropertiesContainer ;
16+ import org .tritonus .share .sampled .TAudioFormat ;
17+ import org .tritonus .share .sampled .file .TAudioFileFormat ;
18+
19+ import javax .naming .OperationNotSupportedException ;
20+ import javax .sound .sampled .AudioFileFormat ;
21+ import javax .sound .sampled .AudioFormat ;
22+ import javax .sound .sampled .AudioInputStream ;
23+ import javax .sound .sampled .AudioSystem ;
24+ import javax .sound .sampled .BooleanControl ;
25+ import javax .sound .sampled .DataLine ;
26+ import javax .sound .sampled .FloatControl ;
27+ import javax .sound .sampled .Line ;
28+ import javax .sound .sampled .LineUnavailableException ;
29+ import javax .sound .sampled .Mixer ;
30+ import javax .sound .sampled .SourceDataLine ;
31+ import javax .sound .sampled .UnsupportedAudioFileException ;
1432import java .io .IOException ;
15- import java .io .InputStream ;
16- import java .net .URL ;
1733import java .nio .ByteBuffer ;
1834import java .nio .ByteOrder ;
1935import java .util .ArrayList ;
2945import java .util .logging .Level ;
3046import java .util .logging .Logger ;
3147
32- import javax .sound .sampled .AudioFileFormat ;
33- import javax .sound .sampled .AudioFormat ;
34- import javax .sound .sampled .AudioInputStream ;
35- import javax .sound .sampled .AudioSystem ;
36- import javax .sound .sampled .BooleanControl ;
37- import javax .sound .sampled .DataLine ;
38- import javax .sound .sampled .FloatControl ;
39- import javax .sound .sampled .Line ;
40- import javax .sound .sampled .LineUnavailableException ;
41- import javax .sound .sampled .Mixer ;
42- import javax .sound .sampled .SourceDataLine ;
43- import javax .sound .sampled .UnsupportedAudioFileException ;
44-
45- import org .tritonus .share .sampled .TAudioFormat ;
46- import org .tritonus .share .sampled .file .TAudioFileFormat ;
47-
48- import com .goxr3plus .streamplayer .enums .AudioType ;
49- import com .goxr3plus .streamplayer .enums .Status ;
50- import com .goxr3plus .streamplayer .stream .StreamPlayerException .PlayerException ;
51- import com .goxr3plus .streamplayer .tools .TimeTool ;
52-
53- import javazoom .spi .PropertiesContainer ;
54-
5548/**
5649 * StreamPlayer is a class based on JavaSound API. It has been successfully tested under Java 10
5750 *
@@ -69,8 +62,10 @@ public class StreamPlayer implements StreamPlayerInterface, Callable<Void> {
6962
7063 private volatile Status status = Status .NOT_SPECIFIED ;
7164
72- /** The data source. */
73- private Object dataSource ;
65+ /**
66+ * The data source
67+ */
68+ private DataSource source ;
7469
7570 /** The audio input stream. */
7671 private volatile AudioInputStream audioInputStream ;
@@ -261,7 +256,12 @@ public void open(final Object object) throws StreamPlayerException {
261256 if (object == null )
262257 return ;
263258
264- dataSource = object ;
259+ //source = new DataSource(object);
260+ try {
261+ source = DataSource .newDataSource (object );
262+ } catch (OperationNotSupportedException e ) {
263+ e .printStackTrace ();
264+ }
265265 initAudioInputStream ();
266266 }
267267
@@ -280,21 +280,13 @@ private void initAudioInputStream() throws StreamPlayerException {
280280
281281 // Notify Status
282282 status = Status .OPENING ;
283- generateEvent (Status .OPENING , getEncodedStreamPosition (), dataSource );
283+ generateEvent (Status .OPENING , getEncodedStreamPosition (), source );
284284
285285 // Audio resources from file||URL||inputStream.
286- if (dataSource instanceof URL ) {
287- audioInputStream = AudioSystem .getAudioInputStream ((URL ) dataSource );
288- audioFileFormat = AudioSystem .getAudioFileFormat ((URL ) dataSource );
289-
290- } else if (dataSource instanceof File ) {
291- audioInputStream = AudioSystem .getAudioInputStream ((File ) dataSource );
292- audioFileFormat = AudioSystem .getAudioFileFormat ((File ) dataSource );
286+ audioInputStream = source .getAudioInputStream ();
293287
294- } else if (dataSource instanceof InputStream ) {
295- audioInputStream = AudioSystem .getAudioInputStream ((InputStream ) dataSource );
296- audioFileFormat = AudioSystem .getAudioFileFormat ((InputStream ) dataSource );
297- }
288+ // Audio resources from file||URL||inputStream.
289+ audioFileFormat = source .getAudioFileFormat ();
298290
299291 // Create the Line
300292 createLine ();
@@ -314,6 +306,7 @@ private void initAudioInputStream() throws StreamPlayerException {
314306 logger .info ("Exited initAudioInputStream\n " );
315307 }
316308
309+
317310 /**
318311 * Determines Properties when the File/URL/InputStream is opened.
319312 */
@@ -367,7 +360,7 @@ private void determineProperties() {
367360 final Map <String , Object > audioPropertiesCopy = audioProperties ; // TODO: Remove, it's meaningless.
368361
369362 // Notify all registered StreamPlayerListeners
370- listeners .forEach (listener -> listener .opened (dataSource , audioPropertiesCopy ));
363+ listeners .forEach (listener -> listener .opened (source . getSource () , audioPropertiesCopy ));
371364
372365 logger .info ("Exited determineProperties()!\n " );
373366
@@ -652,7 +645,7 @@ public long seekBytes(final long bytes) throws StreamPlayerException {
652645 long totalSkipped = 0 ;
653646
654647 // If it is File
655- if (dataSource instanceof File ) {
648+ if (source . isFile () ) {
656649
657650 // Check if the requested bytes are more than totalBytes of Audio
658651 final long bytesLength = getTotalBytes ();
@@ -771,18 +764,7 @@ private void validateSeconds(int seconds, int durationInSeconds) {
771764
772765 @ Override
773766 public int getDurationInSeconds () {
774-
775- // Audio resources from file||URL||inputStream.
776- if (dataSource instanceof File ) {
777- return TimeTool .durationInSeconds (((File ) dataSource ).getAbsolutePath (), AudioType .FILE );
778- } else if (dataSource instanceof URL ) { //todo
779- return -1 ;
780- } else if (dataSource instanceof InputStream ) { //todo
781- return -1 ;
782- }
783-
784- return -1 ;
785-
767+ return source .getDurationInSeconds ();
786768 }
787769
788770 /**
@@ -910,7 +892,7 @@ private void goOutOfPause() {
910892 @ Override
911893 public int getEncodedStreamPosition () {
912894 int position = -1 ;
913- if (dataSource instanceof File && encodedAudioInputStream != null )
895+ if (source . isFile () && encodedAudioInputStream != null )
914896 try {
915897 position = encodedAudioLength - encodedAudioInputStream .available ();
916898 } catch (final IOException ex ) {
0 commit comments