Skip to content

Commit ea4b7d9

Browse files
authored
Merge pull request #18 from HelgeStenstrom/anotherMain
Another main, application and listener implementation
2 parents a4aaa94 + 29c0aa9 commit ea4b7d9

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.goxr3plus.streamplayer.application;
2+
3+
import com.goxr3plus.streamplayer.enums.Status;
4+
import com.goxr3plus.streamplayer.stream.StreamPlayer;
5+
import com.goxr3plus.streamplayer.stream.StreamPlayerEvent;
6+
import com.goxr3plus.streamplayer.stream.StreamPlayerListener;
7+
8+
import java.io.File;
9+
import java.util.Map;
10+
11+
/**
12+
* @author GOXR3PLUS
13+
*
14+
*/
15+
public class AnotherDemoApplication {
16+
17+
private final String audioFileName = "Logic - Ballin [Bass Boosted].mp3";
18+
19+
private StreamPlayer streamPlayer;
20+
private StreamPlayerListener listener;
21+
22+
public AnotherDemoApplication(StreamPlayer streamPlayer) {
23+
this.streamPlayer = streamPlayer;
24+
this.listener = new AnotherStreamPlayerListener(audioFileName, streamPlayer);
25+
26+
}
27+
28+
29+
void start() {
30+
try {
31+
32+
// Register to the Listeners
33+
streamPlayer.addStreamPlayerListener(listener);
34+
35+
// Open a File
36+
// open(new File("...")) //..Here must be the file absolute path
37+
// open(INPUTSTREAM)
38+
// open(AUDIOURL)
39+
40+
// Example
41+
streamPlayer.open(new File(audioFileName));
42+
43+
//Seek by bytes
44+
//seekBytes(500000L);
45+
46+
//Seek +x seconds starting from the current position
47+
streamPlayer.seekSeconds(15); // forward 15 seconds
48+
streamPlayer.seekSeconds(15); // forward 15 seconds again
49+
50+
/* Seek starting from the begginning of the audio */
51+
//seekTo(200);
52+
53+
// Play it
54+
streamPlayer.play();
55+
//pause();
56+
57+
} catch (final Exception ex) {
58+
ex.printStackTrace();
59+
}
60+
}
61+
62+
63+
64+
65+
private String getExtension(String audioFileName) {
66+
return audioFileName.split("\\.(?=[^.]+$)")[1];
67+
}
68+
69+
70+
// public static void main(final String[] args) {
71+
// new AnotherDemoApplication();
72+
// }
73+
74+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.goxr3plus.streamplayer.application;
2+
3+
import com.goxr3plus.streamplayer.stream.StreamPlayer;
4+
import com.goxr3plus.streamplayer.stream.StreamPlayerListener;
5+
6+
public class AnotherMain {
7+
public static void main(String[] args) {
8+
9+
final StreamPlayer streamPlayer = new StreamPlayer();
10+
final AnotherDemoApplication application = new AnotherDemoApplication(streamPlayer);
11+
application.start();
12+
13+
}
14+
15+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.goxr3plus.streamplayer.application;
2+
3+
import com.goxr3plus.streamplayer.enums.Status;
4+
import com.goxr3plus.streamplayer.stream.StreamPlayer;
5+
import com.goxr3plus.streamplayer.stream.StreamPlayerEvent;
6+
import com.goxr3plus.streamplayer.stream.StreamPlayerListener;
7+
8+
import java.util.Map;
9+
10+
class AnotherStreamPlayerListener implements StreamPlayerListener {
11+
12+
private final String audioFileName;
13+
private StreamPlayer streamPlayer;
14+
15+
16+
public AnotherStreamPlayerListener(String audioFileName, StreamPlayer streamPlayer) {
17+
this.audioFileName = audioFileName;
18+
this.streamPlayer = streamPlayer;
19+
}
20+
21+
/**
22+
* It is called when the StreamPlayer open(Object object) method is called.
23+
*
24+
* @param dataSource the data source
25+
* @param properties the properties
26+
*/
27+
@Override
28+
public void opened(Object dataSource, Map<String, Object> properties) {
29+
System.out.println("The StreamPlayer was opened.");
30+
}
31+
32+
/**
33+
* Is called several times per second when StreamPlayer run method is
34+
* running.
35+
*
36+
* @param nEncodedBytes the n encoded bytes
37+
* @param microsecondPosition the microsecond position
38+
* @param pcmData the pcm data
39+
* @param properties the properties
40+
*/
41+
@Override
42+
public void progress(int nEncodedBytes, long microsecondPosition, byte[] pcmData, Map<String, Object> properties) {
43+
44+
String extension = getExtension(audioFileName);
45+
46+
47+
long totalBytes = streamPlayer.getTotalBytes();
48+
if ("mp3".equals(extension) || "wav".equals(extension)) {
49+
50+
// Calculate the progress until now
51+
double progress = (nEncodedBytes > 0 && totalBytes > 0)
52+
? ((double) nEncodedBytes / (double)totalBytes )
53+
: -1.0d;
54+
55+
// TODO: Understand why the nEncodedBytes doesn't update each call of progress.
56+
57+
System.out.println("Seconds : " + (int) (microsecondPosition / 1000000) + " s " + "Progress: [ " + progress * 100 + " ] %");
58+
final String message = String.format("Time: %.1f s, Progress: %.2f %%, encoded %d of %d bytes.",
59+
microsecondPosition / 1000000d,
60+
progress * 100d,
61+
nEncodedBytes,
62+
totalBytes);
63+
System.out.println(message);
64+
}
65+
66+
67+
}
68+
69+
/**
70+
* Is called every time the status of the StreamPlayer changes.
71+
*
72+
* @param event the event
73+
*/
74+
@Override
75+
public void statusUpdated(StreamPlayerEvent event) {
76+
// Player status
77+
final Status status = event.getPlayerStatus();
78+
79+
// Do different things depending on the status.
80+
// See XR3PLAYER https://github.com/goxr3plus/XR3Player for advanced examples
81+
82+
}
83+
84+
private String getExtension(String audioFileName) {
85+
return audioFileName.split("\\.(?=[^.]+$)")[1];
86+
}
87+
88+
}

0 commit comments

Comments
 (0)