我是新来的Android。我创建了Media Player的服务,以便它能够持续播放歌曲,即使我关闭应用程序。我创建活动媒体播放器,它是有一个像播放,暂停,下一个,previous,搜索栏的所有功能,还包括oncompletionlistener。所有做工精良。但现在我想,所有应该由服务来管理。
I am new to Android. I am creating service for Media Player so that it can continue to play song even if i close the application. I have created activity for Media Player and it is having all the functionality like play , pause , next , previous , seekbar and also includes oncompletionlistener . All works excellent. But Now i want that all should be managed by service.
我已经创建的MyService类:
I have Created MyService Class :
public class MyService extends Service {
public static MediaPlayer mp;
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate() {
mp = new MediaPlayer();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
但在我的玩家活动我创建的ArrayList为的SongList从中我正在currentsongIndex并通过它我维护所有的功能,例如接下来,previous所有。现在的服务如何获得的SongList这是还需要在我的活动?我应该在哪里创建MediaPlayer对象是指在服务或活动??
But in my player activity i have created ArrayList for Songlist from which i am taking currentsongIndex and through it i am maintaining all the functionality like next , previous and all.. Now in service how do i get songlist which is also required in my activity ?? Where should i create MediaPlayer object mean in service or activity ??
有关MediaPlayer的我已经下文称http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/ 。对于我的媒体播放器code,你可以参考这个网站。谢谢。 Pleaze清楚我的疑问。我感到很困惑。尽快回复我。
for MediaPlayer I have reffered http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/ . For my media player code you can refer this site. Thanks. Pleaze clear my doubt. I am so confused. Reply me soon..
您是在正确的轨道上。我已经适应了从SDK样本;我这是怎么做到这一点,它的伟大工程。 从你的ArrayList(在你的活动不会从服务)的调用。
You are on the right track. I have adapted from the SDK Samples; this is how I do it and it works great. From your ArrayList (in your activity NOT from the Service) call
onListItemClick
和启动意图启动音乐服务:
and start an intent that starts the music service:
startService(new Intent(MusicService.ACTION_PLAY));
在你的清单,你将需要添加:
In your manifest you will need to add:
<intent-filter>
<action android:name="com.blah.blah.action.PLAY" />
<xxx xxx>
</intent-filter>
当然在你的音乐服务,你需要接受的意图:
And of course in your Music Service you need to receive the Intent:
public int onStartCommand(Intent intent, int flags, int startId) {
String action = intent.getAction();
if (action.equals(ACTION_PLAY))
processPlayRequest();
}
一定要添加意图的跳跃,后退,停止等。 让我知道如果这有助于。
Be sure to add Intents for skip, rewind, stop etc. Let me know if this helps.