Skip to content

Commit b0e40df

Browse files
Kentros AlexandrosKentros Alexandros
authored andcommitted
Implemented seekSeconds(int seconds)
1 parent b527731 commit b0e40df

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

src/main/java/com/goxr3plus/streamplayer/application/Main.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,20 @@ public Main() {
3838
// Example
3939
open(new File(audioAbsolutePath));
4040

41-
//Seek
42-
// seek(500000L);
43-
seekTo(-5);
41+
//Seek by bytes
42+
//seekBytes(500000L);
43+
44+
//Seek +x seconds starting from the current position
45+
seekSeconds(15);
46+
seekSeconds(15);
47+
48+
/* Seek starting from the begginning of the audio */
49+
//seekTo(200);
4450

4551
// Play it
4652
play();
4753
//pause();
4854

49-
50-
5155
} catch (final Exception ex) {
5256
ex.printStackTrace();
5357
}

src/main/java/com/goxr3plus/streamplayer/stream/StreamPlayer.java

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,7 @@ private void awaitTermination() {
679679
*
680680
* @throws StreamPlayerException the stream player exception
681681
*/
682-
public long seek(final long bytes) throws StreamPlayerException {
682+
public long seekBytes(final long bytes) throws StreamPlayerException {
683683
long totalSkipped = 0;
684684

685685
// If it is File
@@ -735,18 +735,27 @@ else if (previousStatus == Status.PAUSED) {
735735
return totalSkipped;
736736
}
737737

738-
// /**
739-
// * Skip x seconds of audio
740-
// * See {@link #seek(long)}
741-
// *
742-
// * @param seconds Seconds to Skip
743-
// */
744-
// public void seekSeconds(int seconds) throws StreamPlayerException {
745-
// long bytes = 0;
746-
//
747-
// seek(bytes);
748-
// }
749-
//
738+
/**
739+
* Skip x seconds of audio
740+
* See {@link #seekBytes(long)}
741+
*
742+
* @param seconds Seconds to Skip
743+
*/
744+
//todo not finished needs more validations
745+
public long seekSeconds(int seconds) throws Exception {
746+
int durationInSeconds = this.getDurationInSeconds();
747+
748+
//Validate
749+
validateSeconds(seconds, durationInSeconds);
750+
751+
//Calculate Bytes
752+
long totalBytes = getTotalBytes();
753+
double percentage = (seconds * 100) / durationInSeconds;
754+
long bytes = (long) (totalBytes * (percentage / 100));
755+
756+
return seekBytes(this.getEncodedStreamPosition() + bytes);
757+
}
758+
750759
// /**
751760
// * Skip seconds of audio based on the pattern
752761
// * See {@link #seek(long)}
@@ -761,25 +770,22 @@ else if (previousStatus == Status.PAUSED) {
761770

762771
/**
763772
* Go to X time of the Audio
764-
* See {@link #seek(long)}
773+
* See {@link #seekBytes(long)}
765774
*
766775
* @param seconds Seconds to Skip
767776
*/
768-
public void seekTo(int seconds) throws Exception {
777+
public long seekTo(int seconds) throws Exception {
769778
int durationInSeconds = this.getDurationInSeconds();
770779

771-
if (seconds < 0) {
772-
throw new Exception("Trying to skip negative seconds ");
773-
} else if (seconds >= durationInSeconds) {
774-
throw new Exception("Trying to skip with seconds {" + seconds + "} > maximum {" + durationInSeconds + "}");
775-
}
780+
//Validate
781+
validateSeconds(seconds, durationInSeconds);
776782

777783
//Calculate Bytes
778784
long totalBytes = getTotalBytes();
779785
double percentage = (seconds * 100) / durationInSeconds;
780-
long seekBytes = (long) (totalBytes * (percentage / 100));
786+
long bytes = (long) (totalBytes * (percentage / 100));
781787

782-
seek(seekBytes);
788+
return seekBytes(bytes);
783789
}
784790

785791
// /**
@@ -794,6 +800,14 @@ public void seekTo(int seconds) throws Exception {
794800
// seek(bytes);
795801
// }
796802

803+
private void validateSeconds(int seconds, int durationInSeconds) throws Exception {
804+
if (seconds < 0) {
805+
throw new Exception("Trying to skip negative seconds ");
806+
} else if (seconds >= durationInSeconds) {
807+
throw new Exception("Trying to skip with seconds {" + seconds + "} > maximum {" + durationInSeconds + "}");
808+
}
809+
}
810+
797811
public int getDurationInSeconds() {
798812

799813
// Audio resources from file||URL||inputStream.

0 commit comments

Comments
 (0)