Android Media Player using service

爷,独闯天下 提交于 2019-12-07 07:15:01

问题


I created a MediaPlayer class to play mp3 files. Everything was fine, then my player stopped playing while it was in background and I found out that my problem was I did not create it in a service, so I started to read about services to learn how to create one and use it for my player.

My question is, what is the best way for me to communicate between the service and my application? Should I use the message or send intents or bind it?

I should also create a UI in notification area, also to show the progress buffering. Do I create a listener from the service side to the activity or there is better way?

I used these examples to learn but I did not learn how to use the onBind method yet:

  • Example: Communication between Activity and Service using Messaging
  • http://www.sapandiwakar.in/technical/tutorial-how-to-manually-create-android-media-player-controls/

Thanks in advance.

EDIT : I need to sometimes ask the player to send me the track details which is playing, and the player to tell me the buffer updates so i can update seek bar. Do I keep sending intents to players or should I use IBinder ?


回答1:


To communicate between service and Activity, I prefer Broadcast Receiver. Send Broadcast from Service and receive it in Activity.




回答2:


Bind your Service to Activity (Say your application), When ever you open it to control MediaPlayer. See Binding Service Tutorial Series. Use foreground service so that its priority always be high and it get killed as a late as possible in memory low sitiuaton



来源:https://stackoverflow.com/questions/18417679/android-media-player-using-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!