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