diff --git a/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayer.java b/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayer.java index efbbf9d..084c514 100644 --- a/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayer.java +++ b/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayer.java @@ -8,8 +8,6 @@ import com.getcapacitor.JSObject; import com.getcapacitor.PluginCall; -import java.util.HashMap; - import dev.eduardoroth.mediaplayer.models.AndroidOptions; import dev.eduardoroth.mediaplayer.models.ExtraOptions; import dev.eduardoroth.mediaplayer.models.MediaPlayerNotification; @@ -27,285 +25,234 @@ public class MediaPlayer { @UnstableApi public void create(PluginCall call, String playerId, String url, AndroidOptions android, ExtraOptions extra) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState != null) { - JSObject ret = new JSObject(); - ret.put("method", "create"); + JSObject ret = new JSObject(); + ret.put("method", "create"); + try { + MediaPlayerStateProvider.getState(playerId); ret.put("result", false); ret.put("message", "Player with id " + playerId + " is already created"); - call.resolve(ret); - return; - } - - _currentActivity.runOnUiThread(() -> { + } catch (Error err) { MediaPlayerContainer playerContainer = new MediaPlayerContainer(_currentActivity, url, playerId, android, extra); _currentActivity.getSupportFragmentManager().beginTransaction().add(R.id.MediaPlayerFragmentContainerView, playerContainer, playerId).commit(); - JSObject ret = new JSObject(); - ret.put("method", "create"); ret.put("result", true); ret.put("value", playerId); - call.resolve(ret); - }); + } + call.resolve(ret); } public void play(PluginCall call, String playerId) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "play"); + JSObject ret = new JSObject(); + ret.put("method", "play"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + playerState.playerController.get().play(); + ret.put("result", true); + ret.put("value", true); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - playerState.playerController.get().play(); - JSObject ret = new JSObject(); - ret.put("method", "play"); - ret.put("result", true); - ret.put("value", true); call.resolve(ret); } public void pause(PluginCall call, String playerId) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "pause"); + JSObject ret = new JSObject(); + ret.put("method", "pause"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + playerState.playerController.get().pause(); + ret.put("result", true); + ret.put("value", true); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - playerState.playerController.get().pause(); - JSObject ret = new JSObject(); - ret.put("method", "pause"); - ret.put("result", true); - ret.put("value", true); call.resolve(ret); } public void getDuration(PluginCall call, String playerId) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "getDuration"); + JSObject ret = new JSObject(); + ret.put("method", "getDuration"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + long duration = playerState.playerController.get().getDuration(); + ret.put("result", true); + ret.put("value", duration == C.TIME_UNSET ? 0 : (duration / 1000)); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - long duration = playerState.playerController.get().getDuration(); - JSObject ret = new JSObject(); - ret.put("method", "getDuration"); - ret.put("result", true); - ret.put("value", duration == C.TIME_UNSET ? 0 : (duration / 1000)); call.resolve(ret); } public void getCurrentTime(PluginCall call, String playerId) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "getCurrentTime"); + JSObject ret = new JSObject(); + ret.put("method", "getCurrentTime"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + long currentTime = playerState.playerController.get().getCurrentTime(); + ret.put("result", true); + ret.put("value", currentTime == C.TIME_UNSET ? 0 : (currentTime / 1000)); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - long currentTime = playerState.playerController.get().getCurrentTime(); - JSObject ret = new JSObject(); - ret.put("method", "getCurrentTime"); - ret.put("result", true); - ret.put("value", currentTime == C.TIME_UNSET ? 0 : (currentTime / 1000)); call.resolve(ret); } public void setCurrentTime(PluginCall call, String playerId, long time) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "setCurrentTime"); + JSObject ret = new JSObject(); + ret.put("method", "setCurrentTime"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + long updatedTime = playerState.playerController.get().setCurrentTime(time); + ret.put("result", true); + ret.put("value", updatedTime); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - long updatedTime = playerState.playerController.get().setCurrentTime(time); - JSObject ret = new JSObject(); - ret.put("method", "setCurrentTime"); - ret.put("result", true); - ret.put("value", updatedTime); call.resolve(ret); } public void isPlaying(PluginCall call, String playerId) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "isPlaying"); + JSObject ret = new JSObject(); + ret.put("method", "isPlaying"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + ret.put("result", true); + ret.put("value", playerState.playerController.get().isPlaying()); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - - JSObject ret = new JSObject(); - ret.put("method", "isPlaying"); - ret.put("result", true); - ret.put("value", playerState.playerController.get().isPlaying()); call.resolve(ret); } public void isMuted(PluginCall call, String playerId) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "isMuted"); + JSObject ret = new JSObject(); + ret.put("method", "isMuted"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + ret.put("result", true); + ret.put("value", playerState.playerController.get().isMuted()); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - JSObject ret = new JSObject(); - ret.put("method", "isMuted"); - ret.put("result", true); - ret.put("value", playerState.playerController.get().isMuted()); call.resolve(ret); } public void mute(PluginCall call, String playerId) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "mute"); + JSObject ret = new JSObject(); + ret.put("method", "mute"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + playerState.playerController.get().mute(); + ret.put("result", true); + ret.put("value", true); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - playerState.playerController.get().mute(); - JSObject ret = new JSObject(); - ret.put("method", "mute"); - ret.put("result", true); - ret.put("value", true); call.resolve(ret); } public void getVolume(PluginCall call, String playerId) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "getVolume"); + JSObject ret = new JSObject(); + ret.put("method", "getVolume"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + ret.put("result", true); + ret.put("value", playerState.playerController.get().getVolume()); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - JSObject ret = new JSObject(); - ret.put("method", "getVolume"); - ret.put("result", true); - ret.put("value", playerState.playerController.get().getVolume()); call.resolve(ret); } public void setVolume(PluginCall call, String playerId, Double volume) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "setVolume"); + JSObject ret = new JSObject(); + ret.put("method", "setVolume"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + playerState.playerController.get().setVolume(volume.floatValue()); + ret.put("result", true); + ret.put("value", volume); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - playerState.playerController.get().setVolume(volume.floatValue()); - JSObject ret = new JSObject(); - ret.put("method", "setVolume"); - ret.put("result", true); - ret.put("value", volume); call.resolve(ret); } public void getRate(PluginCall call, String playerId) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "getRate"); + JSObject ret = new JSObject(); + ret.put("method", "getRate"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + ret.put("result", true); + ret.put("value", playerState.playerController.get().getRate()); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - JSObject ret = new JSObject(); - ret.put("method", "getRate"); - ret.put("result", true); - ret.put("value", playerState.playerController.get().getRate()); call.resolve(ret); } public void setRate(PluginCall call, String playerId, Double rate) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "setRate"); + JSObject ret = new JSObject(); + ret.put("method", "setRate"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + playerState.playerController.get().setRate(rate.floatValue()); + ret.put("result", true); + ret.put("value", rate); + } catch (Error err) { ret.put("result", false); ret.put("message", "Player not found"); - call.resolve(ret); - return; } - playerState.playerController.get().setRate(rate.floatValue()); - JSObject ret = new JSObject(); - ret.put("method", "setRate"); - ret.put("result", true); - ret.put("value", rate); call.resolve(ret); } public void remove(PluginCall call, String playerId) { - MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); - if (playerState == null) { - JSObject ret = new JSObject(); - ret.put("method", "remove"); - ret.put("result", false); - ret.put("message", "Player not found"); - call.resolve(ret); - return; - } - - _currentActivity.runOnUiThread(() -> { + JSObject ret = new JSObject(); + ret.put("method", "remove"); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); Fragment playerFragment = _currentActivity.getSupportFragmentManager().findFragmentByTag(playerId); + playerState.playerController.get().destroy(); + MediaPlayerStateProvider.clearState(playerId); if (playerFragment != null) { _currentActivity.getSupportFragmentManager().beginTransaction().remove(playerFragment).commit(); - MediaPlayerStateProvider.clearState(playerId); } - MediaPlayerNotificationCenter.post( - MediaPlayerNotification.create(playerId, MediaPlayerNotificationCenter.NOTIFICATION_TYPE.MEDIA_PLAYER_REMOVED) - .build() - ); - JSObject ret = new JSObject(); - ret.put("method", "remove"); + MediaPlayerNotificationCenter.post(MediaPlayerNotification.create(playerId, MediaPlayerNotificationCenter.NOTIFICATION_TYPE.MEDIA_PLAYER_REMOVED).build()); ret.put("result", true); ret.put("value", playerId); - call.resolve(ret); - }); + } catch (Error err) { + ret.put("result", false); + ret.put("message", "Player not found"); + } + call.resolve(ret); } public void removeAll(PluginCall call) { - _currentActivity.runOnUiThread(() -> { - _currentActivity.getSupportFragmentManager().getFragments().forEach(fragment -> { - String playerId = fragment.getTag(); - _currentActivity.getSupportFragmentManager().beginTransaction().remove(fragment).commit(); - MediaPlayerStateProvider.clearState(playerId); - MediaPlayerNotificationCenter.post( - MediaPlayerNotification.create(playerId, MediaPlayerNotificationCenter.NOTIFICATION_TYPE.MEDIA_PLAYER_REMOVED) - .build() - ); - }); - JSObject ret = new JSObject(); - ret.put("method", "removeAll"); - ret.put("result", true); - ret.put("value", "[]"); - call.resolve(ret); + _currentActivity.getSupportFragmentManager().getFragments().forEach(fragment -> { + String playerId = fragment.getTag(); + _currentActivity.getSupportFragmentManager().beginTransaction().remove(fragment).commit(); + try { + MediaPlayerState playerState = MediaPlayerStateProvider.getState(playerId); + playerState.playerController.get().destroy(); + } catch (Error ignored) { + } + MediaPlayerStateProvider.clearState(playerId); + MediaPlayerNotificationCenter.post(MediaPlayerNotification.create(playerId, MediaPlayerNotificationCenter.NOTIFICATION_TYPE.MEDIA_PLAYER_REMOVED).build()); }); + JSObject ret = new JSObject(); + ret.put("method", "removeAll"); + ret.put("result", true); + ret.put("value", "[]"); + call.resolve(ret); } } diff --git a/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerContainer.java b/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerContainer.java index 869c13d..19d4def 100644 --- a/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerContainer.java +++ b/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerContainer.java @@ -219,7 +219,7 @@ private ActionBar getSupportActionBar() { } return actionBar; } - + @Override public void onPause() { if (_android.enableBackgroundPlay) { @@ -242,8 +242,10 @@ public void onResume() { super.onResume(); } + @Override public void onDestroy(){ _playerController.destroy(); super.onDestroy(); } + } diff --git a/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerController.java b/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerController.java index 476d4dd..e7e2d11 100644 --- a/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerController.java +++ b/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerController.java @@ -29,6 +29,7 @@ import com.google.android.gms.cast.framework.CastContext; import com.google.android.gms.cast.framework.CastState; +import com.google.android.gms.cast.framework.CastStateListener; import com.google.common.util.concurrent.MoreExecutors; import java.util.ArrayList; @@ -49,12 +50,11 @@ public class MediaPlayerController { private final int _layoutId; private final String _playerId; - private final AndroidOptions _android; private final ExtraOptions _extra; - private final Handler _handler = new Handler(); private final Map _mediaItems = new HashMap<>(); private final Context _context; + private CastContext _castContext = null; private final ExoPlayer _exoPlayer; private final CastPlayer _castPlayer; private final MediaPlayerState _mediaPlayerState; @@ -62,12 +62,13 @@ public class MediaPlayerController { private MediaSession _castPlayerMediaSession; private PlayerNotificationManager _playerNotificationManager; + private final Handler _handlerCurrentTime = new Handler(); + private Player _activePlayer; public MediaPlayerController(Context context, String playerId, AndroidOptions android, ExtraOptions extra) { _layoutId = playerId.chars().reduce(0, Integer::sum); _playerId = playerId; - _android = android; _extra = extra; _context = context; @@ -88,24 +89,6 @@ public MediaPlayerController(Context context, String playerId, AndroidOptions an setActivePlayer(false); } }); - _mediaPlayerState.willBeDestroyed.observe(willBeDestroyed -> { - if (willBeDestroyed) { - _exoPlayer.release(); - _exoPlayerMediaSession.release(); - _exoPlayerMediaSession = null; - - if (_mediaPlayerState.canCast.get()) { - assert _castPlayer != null; - _castPlayer.release(); - _castPlayerMediaSession.release(); - _castPlayerMediaSession = null; - } - - _playerNotificationManager.setPlayer(null); - _playerNotificationManager.invalidate(); - } - }); - } public Player getActivePlayer() { @@ -193,13 +176,21 @@ public boolean shouldShowSubtitles() { } public void destroy() { - _activePlayer.release(); + _exoPlayer.pause(); if (_exoPlayerMediaSession != null) { _exoPlayerMediaSession.release(); } - if (_castPlayerMediaSession != null) { + _exoPlayer.release(); + + if (_mediaPlayerState.canCast.get() && _castPlayer != null) { + _castPlayer.pause(); + _castPlayer.release(); _castPlayerMediaSession.release(); + _castPlayerMediaSession = null; } + + _playerNotificationManager.setPlayer(null); + _playerNotificationManager.invalidate(); } private void setActivePlayer() { @@ -260,46 +251,29 @@ private ExoPlayer createExoPlayer() { exoPlayer.setRepeatMode(_extra.loopOnEnd ? Player.REPEAT_MODE_ONE : Player.REPEAT_MODE_OFF); exoPlayer.setAudioAttributes(new AudioAttributes.Builder().setContentType(C.AUDIO_CONTENT_TYPE_MOVIE).setAllowedCapturePolicy(C.ALLOW_CAPTURE_BY_SYSTEM).setUsage(C.USAGE_MEDIA).build(), true); - /// Listeners - Player.Listener playerListener = new Player.Listener() { + exoPlayer.addListener(new Player.Listener() { @Override public void onPositionDiscontinuity(@NonNull Player.PositionInfo oldPosition, @NonNull Player.PositionInfo newPosition, int reason) { Player.Listener.super.onPositionDiscontinuity(oldPosition, newPosition, reason); if (reason == Player.DISCONTINUITY_REASON_SEEK) { - MediaPlayerNotificationCenter.post( - MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_SEEK) - .addData("previousTime", oldPosition.positionMs / 1000) - .addData("newTime", newPosition.positionMs / 1000) - .build() - ); + MediaPlayerNotificationCenter.post(MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_SEEK).addData("previousTime", oldPosition.positionMs / 1000).addData("newTime", newPosition.positionMs / 1000).build()); } } @Override public void onIsPlayingChanged(boolean isPlaying) { - Player.Listener.super.onIsPlayingChanged(isPlaying); if (isPlaying) { - _handler.postDelayed(new Runnable() { + MediaPlayerNotificationCenter.post(MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_PLAY).build()); + _handlerCurrentTime.postDelayed(new Runnable() { @Override public void run() { - MediaPlayerNotificationCenter.post( - MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_TIME_UPDATED) - .addData("currentTime", exoPlayer.getCurrentPosition() / 1000) - .build() - ); - _handler.postDelayed(this, 100); + MediaPlayerNotificationCenter.post(MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_TIME_UPDATED).addData("currentTime", _exoPlayer.getCurrentPosition() / 1000).build()); + _handlerCurrentTime.postDelayed(this, 100); } }, 100); - MediaPlayerNotificationCenter.post( - MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_PLAY) - .build() - ); } else { - _handler.removeCallbacksAndMessages(null); - MediaPlayerNotificationCenter.post( - MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_PAUSE) - .build() - ); + _handlerCurrentTime.removeCallbacksAndMessages(null); + MediaPlayerNotificationCenter.post(MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_PAUSE).build()); } } @@ -311,24 +285,17 @@ public void onPlaybackStateChanged(int playbackState) { case Player.STATE_IDLE: break; case Player.STATE_ENDED: - MediaPlayerNotificationCenter.post( - MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_ENDED) - .build() - ); + MediaPlayerNotificationCenter.post(MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_ENDED).build()); break; case Player.STATE_READY: - MediaPlayerNotificationCenter.post( - MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_READY) - .build() - ); + MediaPlayerNotificationCenter.post(MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_READY).build()); if (_extra.autoPlayWhenReady) { exoPlayer.play(); } break; } } - }; - exoPlayer.addListener(playerListener); + }); _exoPlayerMediaSession = new MediaSession.Builder(_context, exoPlayer).setPeriodicPositionUpdateEnabled(true).build(); @@ -340,16 +307,15 @@ public void onPlaybackStateChanged(int playbackState) { @OptIn(markerClass = UnstableApi.class) private CastPlayer createCastPlayer() { - CastContext castContext = null; try { - castContext = CastContext.getSharedInstance(_context, MoreExecutors.directExecutor()).getResult(); + _castContext = CastContext.getSharedInstance(_context, MoreExecutors.directExecutor()).getResult(); } catch (RuntimeException ignored) { } - if (castContext == null) { + if (_castContext == null) { return null; } - CastPlayer castPlayer = new CastPlayer(castContext); + CastPlayer castPlayer = new CastPlayer(_castContext); MediaRouter mRouter = MediaRouter.getInstance(_context); MediaRouteSelector mSelector = new MediaRouteSelector.Builder().addControlCategories(Arrays.asList(MediaControlIntent.CATEGORY_LIVE_AUDIO, MediaControlIntent.CATEGORY_LIVE_VIDEO)).build(); @@ -357,7 +323,7 @@ private CastPlayer createCastPlayer() { mRouter.addCallback(mSelector, new MediaRouter.Callback() { }, MediaRouter.CALLBACK_FLAG_REQUEST_DISCOVERY); - castContext.addCastStateListener(state -> _mediaPlayerState.canCast.set(state != CastState.NO_DEVICES_AVAILABLE)); + _castContext.addCastStateListener(state -> _mediaPlayerState.canCast.set(state != CastState.NO_DEVICES_AVAILABLE)); castPlayer.addListener(new CastPlayer.Listener() { @Override @@ -365,19 +331,14 @@ public void onPlaybackStateChanged(int playbackState) { CastPlayer.Listener.super.onPlaybackStateChanged(playbackState); if (playbackState == CastPlayer.STATE_READY) { if (castPlayer.isPlaying()) { - MediaPlayerNotificationCenter.post( - MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_PLAY) - .build() - ); + MediaPlayerNotificationCenter.post(MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_PLAY).build()); } else { - MediaPlayerNotificationCenter.post( - MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_PAUSE) - .build() - ); + MediaPlayerNotificationCenter.post(MediaPlayerNotification.create(_playerId, NOTIFICATION_TYPE.MEDIA_PLAYER_PAUSE).build()); } } } }); + castPlayer.setSessionAvailabilityListener(new SessionAvailabilityListener() { @Override public void onCastSessionAvailable() { diff --git a/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerPlugin.java b/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerPlugin.java index 99e1adc..7b087f4 100644 --- a/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerPlugin.java +++ b/android/src/main/java/dev/eduardoroth/mediaplayer/MediaPlayerPlugin.java @@ -30,9 +30,7 @@ public void load() { bridge.getActivity().getSupportFragmentManager(); implementation = new MediaPlayer(bridge.getActivity()); MediaPlayerNotificationCenter.init(bridge.getActivity()); - MediaPlayerNotificationCenter.listenNotifications(nextNotification -> { - notifyListeners(nextNotification.getEventName(), nextNotification.getData()); - }); + MediaPlayerNotificationCenter.listenNotifications(nextNotification -> notifyListeners(nextNotification.getEventName(), nextNotification.getData())); } @OptIn(markerClass = UnstableApi.class) @@ -87,7 +85,7 @@ public void create(final PluginCall call) { } ExtraOptions extra = new ExtraOptions(extraOptions.getString("title"), extraOptions.getString("subtitle"), getFilePath(extraOptions.getString("poster", null)), extraOptions.getString("artist"), rate, subtitles, extraOptions.optBoolean("autoPlayWhenReady", false), extraOptions.optBoolean("loopOnEnd", false), extraOptions.optBoolean("showControls", true), extraOptions.getJSObject("headers")); - implementation.create(call, playerId, url, android, extra); + bridge.getActivity().runOnUiThread(() -> implementation.create(call, playerId, url, android, extra)); } @PluginMethod @@ -101,12 +99,7 @@ public void play(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.play(call, playerId); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.play(call, playerId)); } @PluginMethod @@ -120,12 +113,7 @@ public void pause(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.pause(call, playerId); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.pause(call, playerId)); } @PluginMethod @@ -139,12 +127,7 @@ public void getDuration(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.getDuration(call, playerId); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.getDuration(call, playerId)); } @PluginMethod @@ -158,12 +141,7 @@ public void getCurrentTime(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.getCurrentTime(call, playerId); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.getCurrentTime(call, playerId)); } @PluginMethod @@ -186,12 +164,7 @@ public void setCurrentTime(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.setCurrentTime(call, playerId, time); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.setCurrentTime(call, playerId, time)); } @PluginMethod @@ -205,12 +178,7 @@ public void isPlaying(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.isPlaying(call, playerId); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.isPlaying(call, playerId)); } @PluginMethod @@ -224,12 +192,7 @@ public void isMuted(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.isMuted(call, playerId); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.isMuted(call, playerId)); } @PluginMethod @@ -239,7 +202,6 @@ public void setVisibilityBackgroundForPiP(final PluginCall call) { ret.put("result", false); ret.put("message", "Method setVisibilityBackgroundForPiP not implemented for Android"); call.resolve(ret); - return; } @PluginMethod @@ -253,12 +215,7 @@ public void mute(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.mute(call, playerId); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.mute(call, playerId)); } @PluginMethod @@ -272,12 +229,7 @@ public void getVolume(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.getVolume(call, playerId); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.getVolume(call, playerId)); } @PluginMethod @@ -300,12 +252,7 @@ public void setVolume(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.setVolume(call, playerId, volume); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.setVolume(call, playerId, volume)); } @PluginMethod @@ -319,12 +266,7 @@ public void getRate(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.getRate(call, playerId); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.getRate(call, playerId)); } @PluginMethod @@ -347,12 +289,7 @@ public void setRate(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.setRate(call, playerId, rate); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.setRate(call, playerId, rate)); } @PluginMethod @@ -366,22 +303,12 @@ public void remove(final PluginCall call) { call.resolve(ret); return; } - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.remove(call, playerId); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.remove(call, playerId)); } @PluginMethod public void removeAll(final PluginCall call) { - bridge.getActivity().runOnUiThread(new Runnable() { - @Override - public void run() { - implementation.removeAll(call); - } - }); + bridge.getActivity().runOnUiThread(() -> implementation.removeAll(call)); } private String getFilePath(String url) { diff --git a/android/src/main/java/dev/eduardoroth/mediaplayer/state/MediaPlayerState.java b/android/src/main/java/dev/eduardoroth/mediaplayer/state/MediaPlayerState.java index 47dcd81..e0d6abd 100644 --- a/android/src/main/java/dev/eduardoroth/mediaplayer/state/MediaPlayerState.java +++ b/android/src/main/java/dev/eduardoroth/mediaplayer/state/MediaPlayerState.java @@ -36,8 +36,6 @@ public enum UI_STATE { public final MediaPlayerStateProperty getCurrentTime; public final MediaPlayerStateProperty getDuration; - public final MediaPlayerStateProperty willBeDestroyed; - public final MediaPlayerStateProperty playerController; public final MediaPlayerStateProperty androidOptions; public final MediaPlayerStateProperty extraOptions; @@ -59,8 +57,6 @@ public MediaPlayerState(LifecycleOwner owner) { getCurrentTime = new MediaPlayerStateProperty<>(owner, 0L); getDuration = new MediaPlayerStateProperty<>(owner, 0L); - willBeDestroyed = new MediaPlayerStateProperty<>(owner, false); - playerController = new MediaPlayerStateProperty<>(owner, null, true); androidOptions = new MediaPlayerStateProperty<>(owner, null, true); extraOptions = new MediaPlayerStateProperty<>(owner, null, true); diff --git a/android/src/main/java/dev/eduardoroth/mediaplayer/state/MediaPlayerStateProvider.java b/android/src/main/java/dev/eduardoroth/mediaplayer/state/MediaPlayerStateProvider.java index 3d1e9ce..42f6c30 100644 --- a/android/src/main/java/dev/eduardoroth/mediaplayer/state/MediaPlayerStateProvider.java +++ b/android/src/main/java/dev/eduardoroth/mediaplayer/state/MediaPlayerStateProvider.java @@ -15,7 +15,7 @@ private MediaPlayerStateProvider() { public static MediaPlayerState getState(String playerId) { if (!_provider._instances.containsKey(playerId)) { - return null; + throw new Error("State not found for playerId " + playerId); } return _provider._instances.get(playerId); }