问题描述
我工作的一个音乐播放器,小部件(主屏幕小部件)..它只需要打1首歌曲,(preferably用的MediaPlayer类)。但我不知道如何实现它。我有点缺乏经验与Android的发展,只是所以这提到的。
类我到目前为止延伸 AppWidgetProvider
,我想这不是一个好主意,让这个类处理音乐播放部分,而是一个服务
。如果是的话,怎么办?
此外,我有3个按钮:播放,暂停,停止,我可以分辨哪一个已经pssed在的onReceive $ P $(...)
在此先感谢!
下面是类。
公共类MusicManager扩展AppWidgetProvider {
私人最终字符串ACTION_WIDGET_PLAY =PlaySong;
私人最终字符串ACTION_WIDGET_PAUSE =PauseSong;
私人最终字符串ACTION_WIDGET_STOP =StopSong;
私人最终诠释INTENT_FLAGS = 0;
私人最终诠释REQUEST_ code = 0;
@覆盖
公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,
INT [] appWidgetIds){
RemoteViews controlButtons =新RemoteViews(context.getPackageName()
R.layout.main);
意图playIntent =新的意图(背景下,MusicService.class);
意图pauseIntent =新的意图(背景下,MusicService.class);
意图stopIntent =新的意图(背景下,MusicService.class);
PendingIntent playPendingIntent = PendingIntent.getService(
背景下,REQUEST_ code,playIntent,INTENT_FLAGS);
PendingIntent pausePendingIntent = PendingIntent.getService(
背景下,REQUEST_ code,pauseIntent,INTENT_FLAGS);
PendingIntent stopPendingIntent = PendingIntent.getService(
背景下,REQUEST_ code,stopIntent,INTENT_FLAGS);
controlButtons.setOnClickPendingIntent(
R.id.btnPlay,playPendingIntent);
controlButtons.setOnClickPendingIntent(
R.id.btnPause,pausePendingIntent);
controlButtons.setOnClickPendingIntent(
R.id.btnStop,stopPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds,controlButtons);
}
}
新增<服务机器人:名称=机器人MusicService。:启用=真/>
到清单!
I'm working on a music player-widget (home screen widget)..It only needs to play 1 song, (Preferably with the MediaPlayer class). But I'm not sure how to implement it. I'm kind of inexperienced with Android development, just so that's mentioned.
The class I have so far extends AppWidgetProvider
, and I guess it is not a good idea to let this class handle the music-playing part, but rather a Service
. And if so, how?
Furthermore, I have 3 buttons: play, pause and stop, and I can distinguish which one has been pressed in onReceive(...)
.
Thanks in advance!
Here is the class.
public class MusicManager extends AppWidgetProvider {
private final String ACTION_WIDGET_PLAY = "PlaySong";
private final String ACTION_WIDGET_PAUSE = "PauseSong";
private final String ACTION_WIDGET_STOP = "StopSong";
private final int INTENT_FLAGS = 0;
private final int REQUEST_CODE = 0;
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
RemoteViews controlButtons = new RemoteViews(context.getPackageName(),
R.layout.main);
Intent playIntent = new Intent(context, MusicService.class);
Intent pauseIntent = new Intent(context, MusicService.class);
Intent stopIntent = new Intent(context, MusicService.class);
PendingIntent playPendingIntent = PendingIntent.getService(
context, REQUEST_CODE, playIntent, INTENT_FLAGS);
PendingIntent pausePendingIntent = PendingIntent.getService(
context, REQUEST_CODE, pauseIntent, INTENT_FLAGS);
PendingIntent stopPendingIntent = PendingIntent.getService(
context, REQUEST_CODE, stopIntent, INTENT_FLAGS);
controlButtons.setOnClickPendingIntent(
R.id.btnPlay, playPendingIntent);
controlButtons.setOnClickPendingIntent(
R.id.btnPause, pausePendingIntent);
controlButtons.setOnClickPendingIntent(
R.id.btnStop, stopPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, controlButtons);
}
}
Added <service android:name=".MusicService" android:enabled="true" />
to the manifest!
这篇关于Android的音乐播放器插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!