3

I want to achieve following goals:

  • Record Audio
  • Send Audio to Server
  • Play Audio

I know that First 2 tasks are possible by Using Service as I've done that in one of the previous apps but when it comes to playing an audio file, it needs to be triggered from an Activity.

Because accessing Activity from Service can be achieved by using BroadcastReceiver but what about accessing method of a Service from an Activity?

So, now comes the question: Whether to go for Service itself for Playing the audio also(triggered from activity) OR to use IntentService.

Documentation says,

No easy or direct way to interact with user interface directly from IntentService.

and

Any tasks started using IntentService cannot be interrupted

I may want to stop recording ant time and play it any time.

Which would suit t he requirement best --> Service OR IntentService ??

Any suggestions will be highly appreciated.

GAMA
  • 5,958
  • 14
  • 79
  • 126
  • You can upload audio using intent service and play your audio on the ui thread in activity. Since the audio is recorded you can save it and play it on the ui thread – Raghunandan Mar 13 '15 at 08:53
  • What will the factor for choosing IntentService over Service for recording and uploading? – GAMA Mar 13 '15 at 08:57
  • read the docs. http://developer.android.com/reference/android/app/IntentService.html – Raghunandan Mar 13 '15 at 08:58
  • But as already mentioned in the question, recording end may be triggered from an activity while doc says "Any tasks started using IntentService cannot be interrupted" – GAMA Mar 13 '15 at 09:03
  • i said use intent service for uploading once audio is recorded. The playing can be done on the ui thread – Raghunandan Mar 13 '15 at 09:04
  • I understood abt playing audio on ui thread. What abt recording it? – GAMA Mar 13 '15 at 09:06
  • @GAMA : **"...playing an audio file, it needs to be triggered from an Activity."** : Why? I don't understand what you mean. – Squonk Mar 13 '15 at 09:06
  • @Squonk - I'll start service from an activity, service will record the audio. But when to play that audio is decided by the service based on some event (Hence, the phrase, triggered from an activity) – GAMA Mar 13 '15 at 09:08
  • @GAMA : OK, when you said "...it needs to be..." I thought you believed it was a restriction of Android but instead you mean it is a requirement of your app design. Correct? – Squonk Mar 13 '15 at 09:11
  • Yes. You understood it correctly. – GAMA Mar 13 '15 at 09:41

1 Answers1

8

I would use a Service rather than an IntentService for what you need.

In particular, use a bound Service which allows two-way communication between the Activity which binds to it and from the Service to the Activity.

The IntentService class is designed for one-shot operations using its own worker thread, once the work on the thread is complete, the thread terminates and the IntentService calls stopSelf() to terminate itself. This means any user interaction between the user (via an Activity) and an IntentService is problematic.

A bound Service on the other hand will exist until it is either explicitly stopped or untill the last bound component unbinds. This allows for longer term interaction.

For playback, the fact a Service runs on the UI thread isn't an issue if you use something like MediaPlayerwhich handles its own thread for playback purposes.

Further to this, if you use a bound Service, MediaPlayer and MediaController, you can control play, stop, pause, seek etc from the Activity.

EDIT: For further information see... Bound Services

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Perfect. Can u give a brief idea abt what you mean by "bound service"? – GAMA Mar 13 '15 at 09:37
  • @GAMA : I can only point you at the guide for bound services on the Android devs site (see the edit it at the bottom of my answer). It looks complex but it's not that bad. Ignore any stuff about IPC and AIDL as they're for accessing Services in other processes. Creating a bound Service in your own app is quite straight-forward. – Squonk Mar 13 '15 at 09:46
  • Will this be a good reference point: http://www.techotopia.com/index.php/Android_Local_Bound_Services_%E2%80%93_A_Worked_Example ?? – GAMA Mar 13 '15 at 09:52
  • @GAMA : Yes, that looks like a good basic explanation and example. Once you have an understanding of the `Binder` and `ServiceConnection` concepts, using bound services is quite straight-forward. Good luck. – Squonk Mar 14 '15 at 01:49
  • @GAMA here's another https://thenewcircle.com/s/post/60/servicesdemo_using_android_services that shows how to have a bounded service. – Raghunandan Mar 18 '15 at 02:00