Search code examples
androidmultithreadingandroid-serviceseekbarandroid-asynctask

I want to play an Audio. Do I use Thread, AsyncTask or Service?


I want to play an audio file in my Activity.

I have three button for controlling the media: play/pause/stop. Additionally, I have a seekbar for the media which I can use to forward/backward the track.

I'm trying to update the seekbar progress every 1 sec. Then, I thought about Threads. Is it the right direction or I need to consider using Services or AsyncTasks ?

Does the MediaPlayer come with a seek bar? This will help me a lot.

Thanks,


Solution

  • I would set up a Service for controlling the media player, and AsyncTask for the application display.

    Have your application send messages to the service to stop, start (using onClick of buttons), and get current position information when it needs to update screen information. This will separate the playing from the controlling.

    Your application can use a simple AsyncTask to pause (SystemClock.sleep(1000)) in its background thread, and update any sliders etc in the update progress thread which runs on the UI. There's no need to write any Thread specific code if you separate the responsibilities in this way.