本文介绍了如何设计一个简单的MP3播放器的活动服务的互动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个Android新手,我想创建一个简单的MP3播放器。现在,据我了解,好办法做到这一点是有这将重新present某种形式的用户界面,并且还派玩说明该服务的服务,将负责的MediaPlayer类以及活动(播放,停止,下一个,等等)。

I am an Android newbie and I wanna create a simple mp3 player. Now, as far as I understood, good way to do this would be to have a service which would be in charge of MediaPlayer class, and an activity which would represent some sort of UI and also send playing instructions to the service (play, stop, next, etc).

我的问题是用什么方法来活动的业务通信?我试图搞清楚这些例子的,但我没有看到我怎么能实现暂停歌曲,接下来,previous等播放器的功能。

My question is what approach to use for activity-service communication? I tried figuring out these examples, but I don't see how I can implement pause song, next, previous etc. features of the player.

所以,我只是感兴趣的概念,也许一些有用的链接,将指导我一下。谢谢!

So I am just interested in concept and maybe some useful links which would guide me a little. Thanks!

推荐答案

步骤#1:实现一个服务

步骤#2:让活动发送指令到服务意图通过交付startService()(除了停,这将是一个调用 stopService())

Step #2: Have the activity send commands to the service in the form of Intents delivered via startService() (except for "stop", which would be a call to stopService()).

第三步:让业务处理的 onStartCommand()这些命令通过的MediaPlayer 正确调用(一定要使用 prepareAsync())。

Step #3: Have the service handle those commands in onStartCommand() by making appropriate calls on MediaPlayer (be sure to use prepareAsync()).

下面是一个的href="https://github.com/commonsguy/cw-android/tree/master/Service/FakePlayer">鸡毛蒜皮的小例子,对于播放和停止,与废止实际的媒体使用。下面是这的稍微复杂的例子,使用通知 startForeground(),以确保玩家可以继续玩。

Here is a trivial little example of this, for play and stop, with stubbed-out actual media usage. Here is a slightly more sophisticated example of this, using a Notification and startForeground() to ensure the player can keep playing.

这篇关于如何设计一个简单的MP3播放器的活动服务的互动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!