Skip to content

Commit c719c07

Browse files
Buthrakaurdoranteseduardo
authored andcommitted
fix AVPlayer random ANR issue when closing the AR scene and disposing the AVPlayer instance (random ANR could be caused by the app waiting for the main thread to complete releasing the AVPlayer instance)
1 parent b80371a commit c719c07

File tree

1 file changed

+21
-4
lines changed
  • android/sharedCode/src/main/java/com/viro/core/internal

1 file changed

+21
-4
lines changed

android/sharedCode/src/main/java/com/viro/core/internal/AVPlayer.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ public interface PlayerAction<T> {
145145
}
146146

147147
private <T> T runSynchronouslyOnMainThread(PlayerAction<T> action) throws ExecutionException, InterruptedException {
148+
return runSynchronouslyOnMainThread(action, true);
149+
}
150+
151+
private <T> T runSynchronouslyOnMainThread(PlayerAction<T> action, boolean waitForResult) throws ExecutionException, InterruptedException {
148152
if (Looper.myLooper() == Looper.getMainLooper()) {
149153
return action.performAction(mExoPlayer);
150154
}
@@ -153,6 +157,9 @@ private <T> T runSynchronouslyOnMainThread(PlayerAction<T> action) throws Execut
153157
FutureTask<T> future = new FutureTask<>(callable);
154158

155159
mainThreadHandler.post(future);
160+
161+
if (!waitForResult) return null;
162+
156163
try {
157164
return future.get();
158165
} catch (Exception e) {
@@ -264,10 +271,20 @@ public void reset() {
264271
}
265272

266273
public void destroy() {
267-
reset();
268-
mExoPlayer.release();
269-
270-
Log.i(TAG, "AVPlayer destroyed");
274+
try {
275+
runSynchronouslyOnMainThread(player -> {
276+
player.stop();
277+
player.seekToDefaultPosition();
278+
player.release();
279+
mState = State.IDLE;
280+
return null;
281+
},
282+
false // don't wait for the result to prevent ANR issue
283+
);
284+
Log.i(TAG, "AVPlayer destroyed");
285+
} catch (Exception e) {
286+
Log.e(TAG, "AVPlayer destroy failed", e);
287+
}
271288
}
272289

273290
public void play() {

0 commit comments

Comments
 (0)