【问题标题】:Android music player widgetAndroid 音乐播放器小部件
【发布时间】:2010-11-28 20:25:32
【问题描述】:

我正在开发音乐播放器小部件(主屏幕小部件).. 它只需要播放一首歌曲,(最好使用 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);         
    }
}

【问题讨论】:

    标签: android widget android-music-player


    【解决方案1】:

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

    【讨论】:

    • 我也和你有同样的要求。你能把你的服务类贴在这里吗?
    • @KeTaN 我希望可以,但我多年前删除了这段代码。对不起!希望你能在别处找到答案。
    • @whirlwin 好的。没问题。无论如何,谢谢。
    • @whirlwin 如果你还记得的话,你还能告诉我,根据你的 MusicService 类,我们可以在我们的服务中编写什么样的代码??
    【解决方案2】:

    AppWidgetProvideronUpdate(...) 方法中,使用类似这样的方法来启动服务(本示例将服务与按钮单击事件相关联):

    Intent intent = new Intent(context, MusicService.class);
    PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
    
    // Get the layout for the App Widget and attach an on-click listener to the button
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.appwidget_provider_layout);
    views.setOnClickPendingIntent(R.id.button, pendingIntent);
    

    更多信息请看here

    【讨论】:

    • 感谢您的回答,但还是不行。我可能在这里遗漏了一些非常明显的东西。
    • 您怎么知道问题不在服务中?让服务在启动时记录一些内容,以便您至少知道它们是否被调用。
    • 我用 Toast 进行了手动调试,但问题是我忘记在清单中包含服务!我恨自己,因为现在..
    猜你喜欢
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-13
    • 1970-01-01
    相关资源
    最近更新 更多