开发者

Passing media player object between activities

I wanted to know how to pass a MediaPlayer object between activities so that I can maint开发者_开发问答ain a constant sound play through out the application.


You don't "pass a MediaPlayer object between activities".

You need to access MediaPlayer from a custom Service to play sound in the background, and have your activities interact with this service.

For interaction my personal preference is binding using AIDL. Whenever I tried to use simple intents and onStartCommand() it revealed insufficient for activity/service interaction (I work with audio too). But it could work for you.

With AIDL and binding, you could expose custom functions such as play(uri), pause(), rewind(), etc.... The MediaPlayer instance would be created in play(uri) if it isn't already instantiated, and released in your service's onDestroy().

You can even register a listener with a RemoteCallbackList to get notified about position changes or some other events. See the examples on the AIDL documentation. What's nice about this is that your service can detect when a listener unregisters. This way you can have a small timeout in the service before you call stopSelf(), if another listener (activity) doesn't register quickly. That's useful because you don't necessarily know if an activity pauses because the user pressed the home button, or because he's cycling from one activity to the other.

Yeah, it needs a little work ;)

Note: you still need to call startService() before binding, otherwise the service will stop whenever an activity unbinds.


Check below link

http://developer.android.com/guide/appendix/faq/framework.html#3

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜