2

ストリーミングメディアプレーヤーウィジェットを作成しようとしています。再生することはできますが、stopを押すと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

自分のアプリでの成功に基づく提案として、appWidgetIdとPendingIntent.FLAG_CANCEL_CURRENTをgetBroadcastパラメーターとして入力してみてください。

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

もちろん、appWidgetIds配列をループする必要があります。

于 2011-04-03T05:10:43.193 に答える
0

わかりました、私は自分で答えを見つけました:上記のコードで私が望んでいたようなメディアプレーヤーを構築することは不可能です。プレーヤーの機能をサービスに入れることにしましたが、今ではそれが魅力のように機能します。名探偵コナン!

于 2011-04-08T07:33:07.390 に答える