Android音乐播放器小部件

时间:2010-11-28 20:25:32

标签: android widget android-music-player

我正在制作音乐播放器小部件(主屏幕小部件).. 它只需要播放1首歌曲(最好使用MediaPlayer类)。但我不确定如何实现它。我对Android开发缺乏经验,就这么说了。

到目前为止,我的课程已扩展为AppWidgetProvider,我想让这个班级处理音乐播放部分不是一个好主意,而是Service。如果是这样,怎么样?

此外,我有3个按钮:播放,暂停和停止,我可以区分onReceive(...)中已按下的按钮。

提前致谢!


这是班级。

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);         
    }
}

2 个答案:

答案 0 :(得分:4)

已添加<service android:name=".MusicService" android:enabled="true" /> 到清单!

答案 1 :(得分:0)

onUpdate(...)的{​​{1}}方法中,使用类似的方法启动服务(此示例将服务与按钮点击事件相关联):

AppWidgetProvider

有关详细信息,请查看here