@@ -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