Android Question Long process in background, best practice

SDFP Studio

Member
Licensed User
Hello,
I have a long process (audio decoding) which can take 30 or more seconds to execute, it is inline java code.
When i call my function it freeze app during execution (so 30 or more seconds).
Is there a solution to do that in background.
Tried thread.RunOnGuiThread but does not execute in background, it freeze app, maybe i don't use it as i should.
Tried thread.start but nothing happens.
If i understand, Erel seems to say that we should not use threads but resumable subs.
So what can i do in such a case ?

Thank you :)
 

sfsameer

Well-Known Member
Licensed User
Longtime User
Hello,
I have a long process (audio decoding) which can take 30 or more seconds to execute, it is inline java code.
When i call my function it freeze app during execution (so 30 or more seconds).
Is there a solution to do that in background.
Tried thread.RunOnGuiThread but does not execute in background, it freeze app, maybe i don't use it as i should.
Tried thread.start but nothing happens.
If i understand, Erel seems to say that we should not use threads but resumable subs.
So what can i do in such a case ?

Thank you :)
Try the below solution :

This is to run the java codes Async meaning a multi thread which will not effect the GUI
:)
 
Last edited:
Upvote 0

SDFP Studio

Member
Licensed User
I use MediaExtractor and MediaCodec to extract the decoded audio from the audio or video, do some calculations and write it to a file.
Media are 3 minutes to 1 hour long, so it takes time.

I started reading your post Libraries and multithreading but it is 'high level' for me so i'm studying it.

Thanks to you and sfsameer for your help.
 
Upvote 0

SDFP Studio

Member
Licensed User
The calculation is very simple, it is an average of the audio samples but:
for 1 min of 44.1 KHz stereo audio, there are 44100 * 2 tracks + 2 bytes (16bits audio) * 60s = 10,584,000 bytes,
then i'm writing to a file (far less data) so i'm not surprised it takes a long time.

I use runAsync and and it's magic, works fine in the background with no effect on the UI, it's perfect :)

Thank you
 
Upvote 0
Top