2

我正在尝试创建一个流媒体播放器小部件。我能够让它播放,但按下停止给(或给)我一个 NullpointerException (但 PAUSE 和 STOP 确实在 Logcat 中很好地显示)。所以 Mediaplayer 启动了,它就消失了,我无法把它拿回来。我做错了一定是小事,但我就是无法摆脱它。任何想法?


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 + "%");
         }

            }

编辑:(编辑撤消)

4

2 回答 2

0

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

于 2011-04-08T07:33:07.390 回答
0

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

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

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

于 2011-04-03T05:10:43.193 回答