私は音楽プレーヤーウィジェット(ホーム画面ウィジェット)に取り組んでいます。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);
}
}