简体   繁体   中英

Android AsyncTask communication for mediaplayer

My task is to download some files and play them sequentially.

Now I have an AsynkTask that call a server, wait until server produce file, and then download it, I do this for n files. There is a final ArrayList in my Activity that is filled by AsyncTask with path of downloaded files.

Another Asynktask wait(using iteration with Thread.Sleep() and max number of iteration) in doInBackground that first element of ArrayList is filled, so it exit from doinbackground and in postexecute() initialize a VideoView with this file and play it, other files are played in OnCompletation method of VideoView.

Now I know that isn't best way to do this, and I'm searching for another way for communicate between Asynctask. I know that using global ArrayList and Thread.Sleep() in a While iteration isn't good. I try using custom callback function, but code of function require UIThread because need to starts VideoView. So I can't call callback from doinBackground().

Any suggestions ?

I don't think if asynctask is the best option for your situation. One of the shortcomings is that you can't really wait in a asynctask for a callback of any sort (unless doing a dirty hack).

The other shortcoming is that by default asynctasks are running in serial, so you can't have one asynctask waiting for the result of another asynctask (this behavior can be changed, but I don't think if this is something you want to do).

A cleaner solution would be using HandlerThread, and then send messages between them. These threads will process a given message, or run a Runnable, and then wait until they get their next message/Runnable. So you can easily setup the communication between them.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM