VJ Vélan Solutions
VJ Vélan Solutions

Reputation: 6554

activity and/or service

I have an activity and a service component in my application.

The intention is to let the service take care of preparing the SurfaceView and mediaplayer instance that needs a surface holder. My media is a video file.

Is it possible to create the surface view from within my service?

Can I call the following

surface = (MySurfaceView) findViewById(R.id.surface);

in my service component?

If I did that, i could then assign the surface.getHolder to the player instance's setDisplay().

The overall goal is to issue commands (play, stop, seek, etc) from the activity and have the service implement the control of the mediaplayer state.

I probably could put all the stuff in my activity, but when i rotate the device, i don't want to tear down the surface and recreate it in onConfigurationChanged(). Hence looking at using the service for my situation here.

Any ideas or solutions highly appreciated.

Thanks.

Upvotes: 1

Views: 1159

Answers (2)

saric
saric

Reputation: 1054

If you want to start and stop Service from the Activity, try using the approach described in this example application. It worked for me.

The demo could be even downloaded. You will need this music file.

Upvotes: 0

rekaszeru
rekaszeru

Reputation: 19220

You could use an AsyncTask implementation instead of the service, and do

  • all the background work in its doInBackground method (different thread, won't bother the user interface), while
  • all the UI-related work could be done in the onPostExecute method (the UI thread).

Update
Since you need your media to be played in a background thread (without possible interruptions by gui processes), the Service could be a choice for you. You'll also need an Activity though, to start, stop and control your service.
For how to implement this design, you can check out this MusicDroid Tutorial part 2.

Though, it's not mandatory, to use services for this task, @CommonsWare's solution is very elegant and handy.
@CommonsWare's streaming video test application, and this Audio/Video player sample don't use services.

Upvotes: 1

Related Questions