-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #18 from HelgeStenstrom/anotherMain
Another main, application and listener implementation
- Loading branch information
Showing
3 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
74 changes: 74 additions & 0 deletions
74
src/main/java/com/goxr3plus/streamplayer/application/AnotherDemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package com.goxr3plus.streamplayer.application; | ||
|
||
import com.goxr3plus.streamplayer.enums.Status; | ||
import com.goxr3plus.streamplayer.stream.StreamPlayer; | ||
import com.goxr3plus.streamplayer.stream.StreamPlayerEvent; | ||
import com.goxr3plus.streamplayer.stream.StreamPlayerListener; | ||
|
||
import java.io.File; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author GOXR3PLUS | ||
* | ||
*/ | ||
public class AnotherDemoApplication { | ||
|
||
private final String audioFileName = "Logic - Ballin [Bass Boosted].mp3"; | ||
|
||
private StreamPlayer streamPlayer; | ||
private StreamPlayerListener listener; | ||
|
||
public AnotherDemoApplication(StreamPlayer streamPlayer) { | ||
this.streamPlayer = streamPlayer; | ||
this.listener = new AnotherStreamPlayerListener(audioFileName, streamPlayer); | ||
|
||
} | ||
|
||
|
||
void start() { | ||
try { | ||
|
||
// Register to the Listeners | ||
streamPlayer.addStreamPlayerListener(listener); | ||
|
||
// Open a File | ||
// open(new File("...")) //..Here must be the file absolute path | ||
// open(INPUTSTREAM) | ||
// open(AUDIOURL) | ||
|
||
// Example | ||
streamPlayer.open(new File(audioFileName)); | ||
|
||
//Seek by bytes | ||
//seekBytes(500000L); | ||
|
||
//Seek +x seconds starting from the current position | ||
streamPlayer.seekSeconds(15); // forward 15 seconds | ||
streamPlayer.seekSeconds(15); // forward 15 seconds again | ||
|
||
/* Seek starting from the begginning of the audio */ | ||
//seekTo(200); | ||
|
||
// Play it | ||
streamPlayer.play(); | ||
//pause(); | ||
|
||
} catch (final Exception ex) { | ||
ex.printStackTrace(); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
private String getExtension(String audioFileName) { | ||
return audioFileName.split("\\.(?=[^.]+$)")[1]; | ||
} | ||
|
||
|
||
// public static void main(final String[] args) { | ||
// new AnotherDemoApplication(); | ||
// } | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/goxr3plus/streamplayer/application/AnotherMain.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.goxr3plus.streamplayer.application; | ||
|
||
import com.goxr3plus.streamplayer.stream.StreamPlayer; | ||
import com.goxr3plus.streamplayer.stream.StreamPlayerListener; | ||
|
||
public class AnotherMain { | ||
public static void main(String[] args) { | ||
|
||
final StreamPlayer streamPlayer = new StreamPlayer(); | ||
final AnotherDemoApplication application = new AnotherDemoApplication(streamPlayer); | ||
application.start(); | ||
|
||
} | ||
|
||
} |
88 changes: 88 additions & 0 deletions
88
src/main/java/com/goxr3plus/streamplayer/application/AnotherStreamPlayerListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.goxr3plus.streamplayer.application; | ||
|
||
import com.goxr3plus.streamplayer.enums.Status; | ||
import com.goxr3plus.streamplayer.stream.StreamPlayer; | ||
import com.goxr3plus.streamplayer.stream.StreamPlayerEvent; | ||
import com.goxr3plus.streamplayer.stream.StreamPlayerListener; | ||
|
||
import java.util.Map; | ||
|
||
class AnotherStreamPlayerListener implements StreamPlayerListener { | ||
|
||
private final String audioFileName; | ||
private StreamPlayer streamPlayer; | ||
|
||
|
||
public AnotherStreamPlayerListener(String audioFileName, StreamPlayer streamPlayer) { | ||
this.audioFileName = audioFileName; | ||
this.streamPlayer = streamPlayer; | ||
} | ||
|
||
/** | ||
* It is called when the StreamPlayer open(Object object) method is called. | ||
* | ||
* @param dataSource the data source | ||
* @param properties the properties | ||
*/ | ||
@Override | ||
public void opened(Object dataSource, Map<String, Object> properties) { | ||
System.out.println("The StreamPlayer was opened."); | ||
} | ||
|
||
/** | ||
* Is called several times per second when StreamPlayer run method is | ||
* running. | ||
* | ||
* @param nEncodedBytes the n encoded bytes | ||
* @param microsecondPosition the microsecond position | ||
* @param pcmData the pcm data | ||
* @param properties the properties | ||
*/ | ||
@Override | ||
public void progress(int nEncodedBytes, long microsecondPosition, byte[] pcmData, Map<String, Object> properties) { | ||
|
||
String extension = getExtension(audioFileName); | ||
|
||
|
||
long totalBytes = streamPlayer.getTotalBytes(); | ||
if ("mp3".equals(extension) || "wav".equals(extension)) { | ||
|
||
// Calculate the progress until now | ||
double progress = (nEncodedBytes > 0 && totalBytes > 0) | ||
? ((double) nEncodedBytes / (double)totalBytes ) | ||
: -1.0d; | ||
|
||
// TODO: Understand why the nEncodedBytes doesn't update each call of progress. | ||
|
||
System.out.println("Seconds : " + (int) (microsecondPosition / 1000000) + " s " + "Progress: [ " + progress * 100 + " ] %"); | ||
final String message = String.format("Time: %.1f s, Progress: %.2f %%, encoded %d of %d bytes.", | ||
microsecondPosition / 1000000d, | ||
progress * 100d, | ||
nEncodedBytes, | ||
totalBytes); | ||
System.out.println(message); | ||
} | ||
|
||
|
||
} | ||
|
||
/** | ||
* Is called every time the status of the StreamPlayer changes. | ||
* | ||
* @param event the event | ||
*/ | ||
@Override | ||
public void statusUpdated(StreamPlayerEvent event) { | ||
// Player status | ||
final Status status = event.getPlayerStatus(); | ||
|
||
// Do different things depending on the status. | ||
// See XR3PLAYER https://github.com/goxr3plus/XR3Player for advanced examples | ||
|
||
} | ||
|
||
private String getExtension(String audioFileName) { | ||
return audioFileName.split("\\.(?=[^.]+$)")[1]; | ||
} | ||
|
||
} |