我正在尝试创建流媒体播放器小部件。我能够播放它,但是按下stop会(或给我)NullpointerException(但在Logcat中会很好地显示PAUSE和STOP)。因此,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 + "%");
}
}
编辑:(撤消编辑)
最佳答案
只是基于我自己的应用程序成功的建议,尝试将appWidgetId和PendingIntent.FLAG_CANCEL_CURRENT放入getBroadcast参数中:
PendingIntent.getBroadcast(context, appWidgetId, play, PendingIntent.FLAG_CANCEL_CURRENT);
当然,您需要遍历appWidgetIds数组。
关于android - Android:Mediaplayer像火箭一样发射,不见了,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5526712/