2

再会

我有一个小部件,我想使用我的小部件播放和停止 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);
 }

}

4

3 回答 3

4

只需不要MediaPlayer在方法中本地声明您的方法,并且仅在它还不是 null 时才对其进行初始化:

 public class MyWidget extends AppWidgetProvider {

  public static String ACTION_WIDGET_RECEIVER = "ActionReceiverWidget";
  private MediaPlayer mp;

  @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)
  {
      if (mp == null)
          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);
 }
}
于 2012-11-05T21:52:34.353 回答
0

要么将 MediaPlayer 变量设为静态,要么从服务播放 mp3 。后者似乎是更好的解决方案。

于 2012-11-05T21:48:59.677 回答
0

非常感谢 !这是我的最终代码

private static MediaPlayer mp;

@Override
  public void onReceive(Context context, Intent intent)
  {
      if (mp == null)
          mp = MediaPlayer.create(context.getApplicationContext(), R.raw.MyMusic);

      final String action = intent.getAction();

      if (ACTION_WIDGET_RECEIVER.equals(action)) {
           if (mp.isPlaying())
           {
               mp.stop();
               mp.release();
               mp = MediaPlayer.create(context.getApplicationContext(), R.raw.MyMusic);

           }
            else
                mp.start();

       }
      super.onReceive(context, intent);
 }
}
          super.onReceive(context, intent);
     }
于 2012-11-06T06:59:23.113 回答