我只是想从锁定屏幕控制媒体播放器的停止/播放。
我找到了两种方法,RemoteControlClient和Mediasession。 但我不明白如何使用它们。 你能告诉我完整的消息来源吗?
如何在我的代码中编写MyRemoteControlEventReceiver类? 实际上AudioBackService类有broadcastReceiver。 如果我使用AudioBackservice作为MyRemoteControlEventReceive吗?
RemoteControlClient示例代码
ComponentName myEventReceiver = new ComponentName(getPackageName(), MyRemoteControlEventReceiver.class.getName());
AudioManager myAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
myAudioManager.registerMediaButtonEventReceiver(myEventReceiver);
// build the PendingIntent for the remote control client
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(myEventReceiver);
PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
// create and register the remote control client
RemoteControlClient myRemoteControlClient = new RemoteControlClient(mediaPendingIntent);
myAudioManager.registerRemoteControlClient(myRemoteControlClient);
我的AudioBackServcie代码(媒体服务)
public class AudioPlaybackService extends Service implements OnPreparedListener, OnCompletionListener
{
final int NOTIFICATION_ID = 1;
public static final String
BROADCAST_PLAYBACK_STOP = "stop"
;
MediaPlayer mediaPlayer;
private String url;
final BroadcastReceiver broadcastReceiver = new BroadcastReceiver()
{
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
if (action.equals(BROADCAST_PLAYBACK_STOP)) stopSelf();
}
};
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
this.url = "http://c5.inlive.co.kr:4282";
Log.d("URL", url);
mediaPlayer.reset();
try
{
mediaPlayer.setDataSource(url);
mediaPlayer.prepareAsync();
showNotification();
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return START_STICKY;
}
@Override
public void onCreate()
{
super.onCreate();
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(this);
mediaPlayer.setOnCompletionListener(this);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(BROADCAST_PLAYBACK_STOP);
registerReceiver(broadcastReceiver, intentFilter);
}
@Override
public void onCompletion(MediaPlayer mp)
{
stopSelf();
}
@Override
public void onPrepared(MediaPlayer mp)
{
Log.d("Service", "MediaPlayer prepared. Music will play now.");
mediaPlayer.start();
}
@Override
public IBinder onBind(Intent arg0)
{
// TODO Auto-generated method stub
return null;
}
private PendingIntent makePendingIntent(String broadcast)
{
Intent intent = new Intent(broadcast);
return PendingIntent.getBroadcast(getApplicationContext(), 0, intent, 0);
}
private void showNotification()
{
// Create notification
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(getString(R.string.app_name))
.setContentText(this.url) // audio url will show in notification
.setContentIntent(PendingIntent.getActivity(getApplicationContext(), 0, new Intent(getApplicationContext(), MainActivity.class), 0));
//.addAction(R.drawable.stop, "Stop", makePendingIntent(BROADCAST_PLAYBACK_STOP));
Notification noti =notificationBuilder.build();
RemoteViews contentiew = new RemoteViews(getPackageName(), R.layout.custom);
contentiew.setOnClickPendingIntent(R.id.button2, makePendingIntent(BROADCAST_PLAYBACK_STOP));
noti.contentView = contentiew;
// Show notification
startForeground(NOTIFICATION_ID,noti );
}
@Override
public void onDestroy() // called when the service is stopped
{
mediaPlayer.stop();
stopForeground(true);
unregisterReceiver(broadcastReceiver);
super.onDestroy();
}
}
谢谢