Android:Mediaplayer像火箭一样发射,不见了

时间:2011-04-03 00:05:48

标签: android android-appwidget

我正在尝试创建一个流媒体播放器小部件。我能够让它播放,但推动停止给我(或给了)一个NullpointerException(但是PAUSE和STOP在Logcat中很好地显示)。因此,媒体播放器开始播出,它已经消失,我无法抢回。它一定是小的,我做错了,但我不能让我的手指背后。有什么想法吗?


public class MyWidget extends AppWidgetProvider implements
MediaPlayer.OnCompletionListener, MediaPlayer.OnPreparedListener,
MediaPlayer.OnErrorListener, MediaPlayer.OnBufferingUpdateListener {

    public static String PLAY = "Play";
    public static String STOP = "Stop";
    public static String PAUSE = "Pause";

     private String TAG = getClass().getSimpleName();
     private MediaPlayer mp;

     @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);

        Intent play = new Intent(context, MyWidget.class);
        play.setAction(PLAY);
        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, play, 0);
        remoteViews.setOnClickPendingIntent(R.id.play, actionPendingIntent);

        play = new Intent(context, MyWidget.class);
        play.setAction(PAUSE);
        actionPendingIntent = PendingIntent.getBroadcast(context, 0, play, 0);
        remoteViews.setOnClickPendingIntent(R.id.pause, actionPendingIntent);

        play = new Intent(context, MyWidget.class);
        play.setAction(STOP);
        actionPendingIntent = PendingIntent.getBroadcast(context, 0, play, 0);
        remoteViews.setOnClickPendingIntent(R.id.stop, actionPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
    }

    @Override
    public void onReceive(Context context, Intent intent) {

        if (intent.getAction().equals(PLAY)) {
            Log.i("onReceive", PLAY);
            play();

        } else if (intent.getAction().equals(PAUSE)) {
            Log.i("onReceive", PAUSE);
            pause();

        } else if (intent.getAction().equals(STOP)) {
            Log.i("onReceive", STOP);
            stop();

        } else {
            super.onReceive(context, intent);
        }


    }

    private void play() {

             try {
           if (mp == null) {
            this.mp = new MediaPlayer();
           } else {
            mp.stop();
            mp.reset();
           }
           mp.setDataSource("MyURL"); // Go to Initialized state
           mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
           mp.setOnPreparedListener(this);
           mp.setOnBufferingUpdateListener(this);

           mp.setOnErrorListener(this);
           mp.prepareAsync();

           Log.d(TAG, "LoadClip Done");
          } catch (Throwable t) {
           Log.d(TAG, t.toString());
          }
         }

         public void onPrepared(MediaPlayer mp) {
          Log.d(TAG, "Stream is prepared");
          try {
          mp.start();
          } catch (Exception e) {
          // 
          }
         }

         private void pause() {
          try {
             mp.pause();
         } catch (Exception e) {
              //
              }
         }

         private void stop() {

             try {
             mp.stop();
             mp.reset();
         } catch (Exception e) {
              // 
              }

         }

         public void onCompletion(MediaPlayer mp) {
          stop();
         }

         public boolean onError(MediaPlayer mp, int what, int extra) {
          StringBuilder sb = new StringBuilder();
          sb.append("Media Player Error: ");
          switch (what) {
          case MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK:
           sb.append("Not Valid for Progressive Playback");
           break;
          case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
           sb.append("Server Died");
           break;
          case MediaPlayer.MEDIA_ERROR_UNKNOWN:
           sb.append("Unknown");
           break;
          default:
           sb.append(" Non standard (");
           sb.append(what);
           sb.append(")");
          }
          sb.append(" (" + what + ") ");
          sb.append(extra);
          Log.e(TAG, sb.toString());
          return true;
         }

         public void onBufferingUpdate(MediaPlayer mp, int percent) {
          Log.d(TAG, "PlayerService onBufferingUpdate : " + percent + "%");
         }

            }

编辑:(编辑撤消)

2 个答案:

答案 0 :(得分:0)

基于我自己的应用程序成功的建议,尝试将appWidgetId和PendingIntent.FLAG_CANCEL_CURRENT作为getBroadcast参数:

PendingIntent.getBroadcast(context, appWidgetId, play, PendingIntent.FLAG_CANCEL_CURRENT);

当然,您需要遍历appWidgetIds数组。

答案 1 :(得分:0)

好的,我自己找到了答案:用上面的代码构建一个像我想要的媒体播放器是不可能的。我决定将播放器功能放在一个服务中,它现在就像一个魅力。结案!