Android music player widget(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 音乐播放器小部件
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01