Android 音乐播放器小部件

Android music player widget(Android 音乐播放器小部件)

本文介绍了Android 音乐播放器小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我目前拥有的类扩展了AppWidgetProvider,我想让这个类处理音乐播放部分不是一个好主意,而是一个Service.如果有,怎么做?

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?

另外,我有3个按钮:播放、暂停和停止,我可以在onReceive(...)中区分哪个按钮被按下了.

Furthermore, I have 3 buttons: play, pause and stop, and I can distinguish which one has been pressed in 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);         
    }
}

推荐答案

添加 到清单!

这篇关于Android 音乐播放器小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android 音乐播放器小部件

基础教程推荐