Skip to content

Commit

Permalink
Merge pull request #18 from HelgeStenstrom/anotherMain
Browse files Browse the repository at this point in the history
Another main, application and listener implementation
  • Loading branch information
goxr3plus authored Sep 4, 2019
2 parents a4aaa94 + 29c0aa9 commit ea4b7d9
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 0 deletions.
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();
// }

}
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();

}

}
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];
}

}

0 comments on commit ea4b7d9

Please sign in to comment.