Skip to content

fix(android): resolve MediaPlayer lifecycle bugs and remove deprecated Android style usage#80

Open
MattyB95 wants to merge 2 commits intomoust:masterfrom
MattyB95:bugfix/android-player-lifecycle
Open

fix(android): resolve MediaPlayer lifecycle bugs and remove deprecated Android style usage#80
MattyB95 wants to merge 2 commits intomoust:masterfrom
MattyB95:bugfix/android-player-lifecycle

Conversation

@MattyB95
Copy link
Copy Markdown
Contributor

Summary

This PR fixes several Android MediaPlayer lifecycle issues that could cause crashes, duplicate callbacks, silent failures, and resource leaks. It also removes deprecated style usage and adds regression tests for the corrected error-handling paths.

Changes

  • Replace deprecated Theme_NoTitleBar with Theme_Black_NoTitleBar_Fullscreen
  • Replace blocking prepare() with prepareAsync()
  • Fix onError() returning false, which triggered onCompletion() and caused double release() / dismiss()
  • Fix missing JavaScript error callback invocation in onError()
  • Null out player and dialog after release to prevent duplicate callbacks and potential NPEs
  • Explicitly close AssetFileDescriptor after asset playback
  • Add returnDefaultValues = true to support Android framework stubbed paths in unit tests
  • Add VideoPlayerOnErrorTest regression coverage for the fixed onError() behaviour

Fixes #71 #73 #75

Copilot AI review requested due to automatic review settings March 15, 2026 02:52
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the Android MediaPlayer lifecycle in the Cordova VideoPlayer plugin (preventing double-callbacks/leaks/crashes), removes deprecated fullscreen handling, and adds an Android Gradle-based unit test harness with regression coverage.

Changes:

  • Fix Android playback lifecycle behavior (async prepare, error handling, safe release/dismiss, asset FD closure, API-safe fullscreen).
  • Add Android unit tests and a standalone Gradle test project under tests/android.
  • Update docs/metadata (Android-only support, permissions guidance, npm/just test commands, cleanup .gitignore).

Reviewed changes

Copilot reviewed 14 out of 16 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
src/android/VideoPlayer.java Lifecycle fixes: async prepare, safer close/error/completion cleanup, API-safe fullscreen via reflection, shared sendError helper.
www/videoplayer.js Replace custom merge with Object.assign-based options merge.
tests/android/build.gradle Introduces Android Gradle test module compiling plugin source and running JUnit/Mockito tests.
tests/android/gradlew Adds Gradle wrapper script for test project.
tests/android/gradle/wrapper/* Adds wrapper jar/properties for pinned Gradle distribution.
tests/android/src/test/java/* Adds regression tests for stripFileProtocol, applyFullscreen, and onError behavior.
tests/android/src/test/resources/...MockMaker Configures Mockito mock maker for unit tests.
tests/android/src/main/AndroidManifest.xml Minimal manifest for the test module.
tests/android/settings.gradle Settings for the test Gradle project.
package.json Updates metadata (Android-only) and adds npm test to run Android unit tests.
README.md Updates docs (Android-only, permissions, troubleshooting).
.gitignore Replaces legacy ignores with common OS/editor/Android/Node ignores.
justfile Adds convenience tasks for running tests/lint/report/validation.
Comments suppressed due to low confidence (1)

src/android/VideoPlayer.java:278

  • prepareAsync() makes onPrepared() asynchronous relative to close() / surfaceDestroyed() which can release/null out the player. As written, mp.start() can throw IllegalStateException if the player was released before the callback fires. Add a guard (e.g., return if player == null or mp != player) and/or wrap start() in a try/catch to avoid lifecycle race crashes.
    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();
    }

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@MattyB95 MattyB95 force-pushed the bugfix/android-player-lifecycle branch from 66b4c5e to 782b3ff Compare March 15, 2026 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Pull-request #70 breaks build for application that target older Android version

2 participants