Skip to content

Commit

Permalink
fix AVPlayer random ANR issue when closing the AR scene and disposing…
Browse files Browse the repository at this point in the history
… the AVPlayer instance (random ANR could be caused by the app waiting for the main thread to complete releasing the AVPlayer instance)
  • Loading branch information
Buthrakaur authored and doranteseduardo committed May 31, 2024
1 parent b80371a commit c719c07
Showing 1 changed file with 21 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ public interface PlayerAction<T> {
}

private <T> T runSynchronouslyOnMainThread(PlayerAction<T> action) throws ExecutionException, InterruptedException {
return runSynchronouslyOnMainThread(action, true);
}

private <T> T runSynchronouslyOnMainThread(PlayerAction<T> action, boolean waitForResult) throws ExecutionException, InterruptedException {
if (Looper.myLooper() == Looper.getMainLooper()) {
return action.performAction(mExoPlayer);
}
Expand All @@ -153,6 +157,9 @@ private <T> T runSynchronouslyOnMainThread(PlayerAction<T> action) throws Execut
FutureTask<T> future = new FutureTask<>(callable);

mainThreadHandler.post(future);

if (!waitForResult) return null;

try {
return future.get();
} catch (Exception e) {
Expand Down Expand Up @@ -264,10 +271,20 @@ public void reset() {
}

public void destroy() {
reset();
mExoPlayer.release();

Log.i(TAG, "AVPlayer destroyed");
try {
runSynchronouslyOnMainThread(player -> {
player.stop();
player.seekToDefaultPosition();
player.release();
mState = State.IDLE;
return null;
},
false // don't wait for the result to prevent ANR issue
);
Log.i(TAG, "AVPlayer destroyed");
} catch (Exception e) {
Log.e(TAG, "AVPlayer destroy failed", e);
}
}

public void play() {
Expand Down

0 comments on commit c719c07

Please sign in to comment.