10
10
11
11
package com .goxr3plus .streamplayer .stream ;
12
12
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 ;
14
32
import java .io .IOException ;
15
- import java .io .InputStream ;
16
- import java .net .URL ;
17
33
import java .nio .ByteBuffer ;
18
34
import java .nio .ByteOrder ;
19
35
import java .util .ArrayList ;
29
45
import java .util .logging .Level ;
30
46
import java .util .logging .Logger ;
31
47
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
-
55
48
/**
56
49
* StreamPlayer is a class based on JavaSound API. It has been successfully tested under Java 10
57
50
*
@@ -69,8 +62,10 @@ public class StreamPlayer implements StreamPlayerInterface, Callable<Void> {
69
62
70
63
private volatile Status status = Status .NOT_SPECIFIED ;
71
64
72
- /** The data source. */
73
- private Object dataSource ;
65
+ /**
66
+ * The data source
67
+ */
68
+ private DataSource source ;
74
69
75
70
/** The audio input stream. */
76
71
private volatile AudioInputStream audioInputStream ;
@@ -261,7 +256,12 @@ public void open(final Object object) throws StreamPlayerException {
261
256
if (object == null )
262
257
return ;
263
258
264
- dataSource = object ;
259
+ //source = new DataSource(object);
260
+ try {
261
+ source = DataSource .newDataSource (object );
262
+ } catch (OperationNotSupportedException e ) {
263
+ e .printStackTrace ();
264
+ }
265
265
initAudioInputStream ();
266
266
}
267
267
@@ -280,21 +280,13 @@ private void initAudioInputStream() throws StreamPlayerException {
280
280
281
281
// Notify Status
282
282
status = Status .OPENING ;
283
- generateEvent (Status .OPENING , getEncodedStreamPosition (), dataSource );
283
+ generateEvent (Status .OPENING , getEncodedStreamPosition (), source );
284
284
285
285
// 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 ();
293
287
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 ();
298
290
299
291
// Create the Line
300
292
createLine ();
@@ -314,6 +306,7 @@ private void initAudioInputStream() throws StreamPlayerException {
314
306
logger .info ("Exited initAudioInputStream\n " );
315
307
}
316
308
309
+
317
310
/**
318
311
* Determines Properties when the File/URL/InputStream is opened.
319
312
*/
@@ -367,7 +360,7 @@ private void determineProperties() {
367
360
final Map <String , Object > audioPropertiesCopy = audioProperties ; // TODO: Remove, it's meaningless.
368
361
369
362
// Notify all registered StreamPlayerListeners
370
- listeners .forEach (listener -> listener .opened (dataSource , audioPropertiesCopy ));
363
+ listeners .forEach (listener -> listener .opened (source . getSource () , audioPropertiesCopy ));
371
364
372
365
logger .info ("Exited determineProperties()!\n " );
373
366
@@ -652,7 +645,7 @@ public long seekBytes(final long bytes) throws StreamPlayerException {
652
645
long totalSkipped = 0 ;
653
646
654
647
// If it is File
655
- if (dataSource instanceof File ) {
648
+ if (source . isFile () ) {
656
649
657
650
// Check if the requested bytes are more than totalBytes of Audio
658
651
final long bytesLength = getTotalBytes ();
@@ -771,18 +764,7 @@ private void validateSeconds(int seconds, int durationInSeconds) {
771
764
772
765
@ Override
773
766
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 ();
786
768
}
787
769
788
770
/**
@@ -910,7 +892,7 @@ private void goOutOfPause() {
910
892
@ Override
911
893
public int getEncodedStreamPosition () {
912
894
int position = -1 ;
913
- if (dataSource instanceof File && encodedAudioInputStream != null )
895
+ if (source . isFile () && encodedAudioInputStream != null )
914
896
try {
915
897
position = encodedAudioLength - encodedAudioInputStream .available ();
916
898
} catch (final IOException ex ) {
0 commit comments