Android,如何在Widget中只使用一个MediaPlayer实例

时间:2012-11-05 21:37:26

标签: android widget media-player

美好的一天

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

}

3 个答案:

答案 0 :(得分:4)

只是不要在方法中本地声明MediaPlayer,只有在它不是空时才初始化它:

 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);
 }
}

答案 1 :(得分:0)

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

答案 2 :(得分: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);
     }