Android Music Playerウィジェット
-
29-09-2019 - |
質問
私はMusic Player-Widget(ホーム画面ウィジェット)に取り組んでいます。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);
}
}
解決
追加した <service android:name=".MusicService" android:enabled="true" />
マニフェストに!
他のヒント
の中に onUpdate(...)
あなたの方法 AppWidgetProvider
, 、このようなものを使用してサービスを開始します(この例は、サービスをボタンクリックイベントに関連付けます):
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);
詳細については、ご覧ください ここ
所属していません StackOverflow