Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4332b54

Browse files
committedAug 7, 2019
fixing ANR on large files
see https://stackoverflow.com/q/57067507/192373 1) use StringBuilder to accumulate the output text 2) convert it to String inside doInBackground, not on Main thread
1 parent 37f8ee3 commit 4332b54

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed
 

‎android-ffmpeg/src/main/java/nl/bravobit/ffmpeg/FFcommandExecuteAsyncTask.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class FFcommandExecuteAsyncTask extends AsyncTask<Void, String, CommandResult> i
1818
private final long timeout;
1919
private long startTime;
2020
private Process process;
21+
private StringBuilder outputStringBuilder = new StringBuilder();
2122
private String output = "";
2223
private boolean quitPending;
2324

@@ -39,23 +40,27 @@ protected void onPreExecute() {
3940

4041
@Override
4142
protected CommandResult doInBackground(Void... params) {
43+
CommandResult ret = CommandResult.getDummyFailureResponse();
4244
try {
4345
process = shellCommand.run(cmd, environment);
4446
if (process == null) {
4547
return CommandResult.getDummyFailureResponse();
4648
}
4749
Log.d("Running publishing updates method");
4850
checkAndUpdateProcess();
49-
return CommandResult.getOutputFromProcess(process);
51+
ret = CommandResult.getOutputFromProcess(process);
52+
outputStringBuilder.append(ret.output);
5053
} catch (TimeoutException e) {
5154
Log.e("FFmpeg binary timed out", e);
52-
return new CommandResult(false, e.getMessage());
55+
ret = new CommandResult(false, e.getMessage());
56+
outputStringBuilder.append(ret.output);
5357
} catch (Exception e) {
5458
Log.e("Error running FFmpeg binary", e);
5559
} finally {
5660
Util.destroyProcess(process);
5761
}
58-
return CommandResult.getDummyFailureResponse();
62+
output = outputStringBuilder.toString();
63+
return ret;
5964
}
6065

6166
@Override
@@ -68,7 +73,6 @@ protected void onProgressUpdate(String... values) {
6873
@Override
6974
protected void onPostExecute(CommandResult commandResult) {
7075
if (ffmpegExecuteResponseHandler != null) {
71-
output += commandResult.output;
7276
if (commandResult.success) {
7377
ffmpegExecuteResponseHandler.onSuccess(output);
7478
} else {
@@ -107,7 +111,7 @@ private void checkAndUpdateProcess() throws TimeoutException, InterruptedExcepti
107111
return;
108112
}
109113

110-
output += line + "\n";
114+
outputStringBuilder.append(line); outputStringBuilder.append("\n");
111115
publishProgress(line);
112116
}
113117
} catch (IOException e) {

0 commit comments

Comments
 (0)
Please sign in to comment.