From 6b363defc08d96115ca8a63537c15c348f4bde65 Mon Sep 17 00:00:00 2001 From: Gilad Nadav Date: Wed, 29 Mar 2017 20:29:25 +0300 Subject: [PATCH] VE-93 - add reset player API (#188) * add reset API to player so app can reset player state before change media * add reset to player API so app that wants to change media can call reset before so player state will be in the initial state * codacy * change API to be called stop add stopped event to this operation --- .../java/com/kaltura/playkit/plugins/MockPlayer.java | 5 +++++ playkit/src/main/java/com/kaltura/playkit/Player.java | 6 ++++++ .../java/com/kaltura/playkit/PlayerDecoratorBase.java | 5 +++++ .../src/main/java/com/kaltura/playkit/PlayerEvent.java | 3 ++- .../com/kaltura/playkit/player/ExoPlayerWrapper.java | 10 ++++++++++ .../com/kaltura/playkit/player/MediaPlayerWrapper.java | 10 ++++++++++ .../com/kaltura/playkit/player/PlayerController.java | 5 +++++ .../java/com/kaltura/playkit/player/PlayerEngine.java | 7 +++++++ 8 files changed, 50 insertions(+), 1 deletion(-) diff --git a/playkit/src/androidTest/java/com/kaltura/playkit/plugins/MockPlayer.java b/playkit/src/androidTest/java/com/kaltura/playkit/plugins/MockPlayer.java index 35206186b..6af689210 100644 --- a/playkit/src/androidTest/java/com/kaltura/playkit/plugins/MockPlayer.java +++ b/playkit/src/androidTest/java/com/kaltura/playkit/plugins/MockPlayer.java @@ -53,6 +53,11 @@ public void destroy() { } + @Override + public void stop() { + // stop player + } + @Override public void play() { isPlaying = true; diff --git a/playkit/src/main/java/com/kaltura/playkit/Player.java b/playkit/src/main/java/com/kaltura/playkit/Player.java index 200387ec2..8de452119 100644 --- a/playkit/src/main/java/com/kaltura/playkit/Player.java +++ b/playkit/src/main/java/com/kaltura/playkit/Player.java @@ -46,6 +46,12 @@ public interface Player { * Should be called when you want to destroy the player. */ void destroy(); + + /** + * stop player and back to initial playback state. + */ + void stop(); + /** * Start playback of the media. */ diff --git a/playkit/src/main/java/com/kaltura/playkit/PlayerDecoratorBase.java b/playkit/src/main/java/com/kaltura/playkit/PlayerDecoratorBase.java index 22016bc00..255ab5411 100644 --- a/playkit/src/main/java/com/kaltura/playkit/PlayerDecoratorBase.java +++ b/playkit/src/main/java/com/kaltura/playkit/PlayerDecoratorBase.java @@ -73,6 +73,11 @@ public void destroy() { player.destroy(); } + @Override + public void stop() { + player.stop(); + } + @Override public PlayerView getView() { return player.getView(); diff --git a/playkit/src/main/java/com/kaltura/playkit/PlayerEvent.java b/playkit/src/main/java/com/kaltura/playkit/PlayerEvent.java index fc4b0f789..daaa803f2 100644 --- a/playkit/src/main/java/com/kaltura/playkit/PlayerEvent.java +++ b/playkit/src/main/java/com/kaltura/playkit/PlayerEvent.java @@ -118,7 +118,8 @@ public enum Type { TRACKS_AVAILABLE, // Sent when track info is available. REPLAY, //Sent when replay happened. PLAYBACK_PARAMS_UPDATED, // Sent event that notify about changes in the playback parameters. When bitrate of the video or audio track changes or new media loaded. Holds the PlaybackParamsInfo.java object with relevant data. - VOLUME_CHANGED // Sent when volume is changed. + VOLUME_CHANGED, // Sent when volume is changed. + STOPPED // sent when stop player api is called } @Override diff --git a/playkit/src/main/java/com/kaltura/playkit/player/ExoPlayerWrapper.java b/playkit/src/main/java/com/kaltura/playkit/player/ExoPlayerWrapper.java index c48173b6e..4edc4bfcc 100644 --- a/playkit/src/main/java/com/kaltura/playkit/player/ExoPlayerWrapper.java +++ b/playkit/src/main/java/com/kaltura/playkit/player/ExoPlayerWrapper.java @@ -560,6 +560,16 @@ public PlayerEvent.ExceptionInfo getCurrentException() { return new PlayerEvent.ExceptionInfo(currentException, sameErrorOccurrenceCounter); } + @Override + public void stop() { + if (player != null) { + player.setPlayWhenReady(false); + player.seekTo(0); + player.stop(); + sendDistinctEvent(PlayerEvent.Type.STOPPED); + } + } + void savePlayerPosition() { if (player == null) { log.e("Attempt to invoke 'savePlayerPosition()' on null instance of the exoplayer"); diff --git a/playkit/src/main/java/com/kaltura/playkit/player/MediaPlayerWrapper.java b/playkit/src/main/java/com/kaltura/playkit/player/MediaPlayerWrapper.java index 855fc5dc6..d6c03c26d 100644 --- a/playkit/src/main/java/com/kaltura/playkit/player/MediaPlayerWrapper.java +++ b/playkit/src/main/java/com/kaltura/playkit/player/MediaPlayerWrapper.java @@ -353,6 +353,16 @@ public PlayerEvent.ExceptionInfo getCurrentException() { return null; } + @Override + public void stop() { + if (player != null) { + player.pause(); + player.seekTo(0); + player.reset(); + sendDistinctEvent(PlayerEvent.Type.STOPPED); + } + } + public static String getWidevineAssetPlaybackUri(String assetUri) { String assetUriForPlayback = assetUri; if (assetUri.startsWith("file:")) { diff --git a/playkit/src/main/java/com/kaltura/playkit/player/PlayerController.java b/playkit/src/main/java/com/kaltura/playkit/player/PlayerController.java index b6950f477..5c77f6ffa 100644 --- a/playkit/src/main/java/com/kaltura/playkit/player/PlayerController.java +++ b/playkit/src/main/java/com/kaltura/playkit/player/PlayerController.java @@ -218,6 +218,11 @@ public void destroy() { eventListener = null; } + @Override + public void stop() { + player.stop(); + } + private void startPlaybackFrom(long startPosition) { if (player == null) { log.e("Attempt to invoke 'startPlaybackFrom()' on null instance of the player engine"); diff --git a/playkit/src/main/java/com/kaltura/playkit/player/PlayerEngine.java b/playkit/src/main/java/com/kaltura/playkit/player/PlayerEngine.java index e4fb9bf22..80a1cf141 100644 --- a/playkit/src/main/java/com/kaltura/playkit/player/PlayerEngine.java +++ b/playkit/src/main/java/com/kaltura/playkit/player/PlayerEngine.java @@ -167,4 +167,11 @@ interface PlayerEngine { * @return - the last {@link PlayerEvent.ExceptionInfo} that happened. */ PlayerEvent.ExceptionInfo getCurrentException(); + + + /** + * Stop player executing the {@link PlayerEngine} implementation. + * stop the player and seek to start position. + */ + void stop(); }