再会
我有一个小部件,我想使用我的小部件播放和停止 mp3 文件。我可以播放 mp3 文件,但我不能停止 :(
关键时刻 - 它是小部件!我的问题的原因 - 我不知道如何只使用一个媒体播放器实例
你有什么想法?
这是我的代码
public class MyWidget extends AppWidgetProvider {
public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
Intent active = new Intent(context, HappyBabyWidget.class);
active.setAction(ACTION_WIDGET_RECEIVER);
PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, active, 0);
remoteViews.setOnClickPendingIntent(R.id.ButtonPlay, actionPendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
@Override
public void onReceive(Context context, Intent intent)
{
MediaPlayer mp = MediaPlayer.create(context.getApplicationContext(), R.raw.MyMusic);
final String action = intent.getAction();
if (ACTION_WIDGET_RECEIVER.equals(action)) {
if (mp.isPlaying())
mp.stop();
else
mp.start();
}
super.onReceive(context, intent);
}
}