use of android.support.v4.media.session.MediaSessionCompat in project MusicLake by caiyonglong.
the class MediaSessionManager method setupMediaSession.
/**
* 初始化并激活 MediaSession
*/
private void setupMediaSession() {
// 第二个参数 tag: 这个是用于调试用的,随便填写即可
mMediaSession = new MediaSessionCompat(context, TAG);
// 指明支持的按键信息类型
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setCallback(callback, mHandler);
mMediaSession.setActive(true);
}
use of android.support.v4.media.session.MediaSessionCompat in project kdeconnect-android by KDE.
the class PresenterActivity method createMediaSession.
private void createMediaSession() {
mMediaSession = new MediaSessionCompat(this, "kdeconnect");
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setPlaybackState(new PlaybackStateCompat.Builder().setState(PlaybackStateCompat.STATE_PLAYING, 0, 0).build());
mMediaSession.setPlaybackToRemote(getVolumeProvider());
mMediaSession.setActive(true);
}
use of android.support.v4.media.session.MediaSessionCompat in project AntennaPod by AntennaPod.
the class PlaybackService method onCreate.
@Override
public void onCreate() {
super.onCreate();
Log.d(TAG, "Service created.");
isRunning = true;
stateManager = new PlaybackServiceStateManager(this);
notificationBuilder = new PlaybackServiceNotificationBuilder(this);
registerReceiver(autoStateUpdated, new IntentFilter("com.google.android.gms.car.media.STATUS"));
registerReceiver(headsetDisconnected, new IntentFilter(Intent.ACTION_HEADSET_PLUG));
registerReceiver(shutdownReceiver, new IntentFilter(ACTION_SHUTDOWN_PLAYBACK_SERVICE));
registerReceiver(bluetoothStateUpdated, new IntentFilter(BluetoothA2dp.ACTION_CONNECTION_STATE_CHANGED));
registerReceiver(audioBecomingNoisy, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
registerReceiver(skipCurrentEpisodeReceiver, new IntentFilter(ACTION_SKIP_CURRENT_EPISODE));
registerReceiver(pausePlayCurrentEpisodeReceiver, new IntentFilter(ACTION_PAUSE_PLAY_CURRENT_EPISODE));
EventBus.getDefault().register(this);
taskManager = new PlaybackServiceTaskManager(this, taskManagerCallback);
PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(prefListener);
ComponentName eventReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setComponent(eventReceiver);
PendingIntent buttonReceiverIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, PendingIntent.FLAG_UPDATE_CURRENT | (Build.VERSION.SDK_INT >= 31 ? PendingIntent.FLAG_MUTABLE : 0));
mediaSession = new MediaSessionCompat(getApplicationContext(), TAG, eventReceiver, buttonReceiverIntent);
setSessionToken(mediaSession.getSessionToken());
try {
mediaSession.setCallback(sessionCallback);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
} catch (NullPointerException npe) {
// on some devices (Huawei) setting active can cause a NullPointerException
// even with correct use of the api.
// See http://stackoverflow.com/questions/31556679/android-huawei-mediassessioncompat
// and https://plus.google.com/+IanLake/posts/YgdTkKFxz7d
Log.e(TAG, "NullPointerException while setting up MediaSession");
npe.printStackTrace();
}
recreateMediaPlayer();
mediaSession.setActive(true);
castStateListener = new CastStateListener(this) {
@Override
public void onSessionStartedOrEnded() {
recreateMediaPlayer();
}
};
EventBus.getDefault().post(new PlaybackServiceEvent(PlaybackServiceEvent.Action.SERVICE_STARTED));
}
use of android.support.v4.media.session.MediaSessionCompat in project SpotiQ by ZinoKader.
the class NotificationUtil method buildPlayerNotificationCompat.
public static Notification buildPlayerNotificationCompat(Context context, MediaSessionCompat mediaSessionCompat, String title, String description, Bitmap largeIcon) {
PendingIntent openPartyIntent = PendingIntent.getActivity(context, 0, new Intent(context, PartyActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
Intent playPauseActionIntent = new Intent(context, SpotiqHostService.class);
playPauseActionIntent.setAction(ServiceConstants.ACTION_PLAY_PAUSE);
PendingIntent playPauseIntent = PendingIntent.getService(context, 1, playPauseActionIntent, PendingIntent.FLAG_UPDATE_CURRENT);
int largeIconWidth = context.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_width);
int largeIconHeight = context.getResources().getDimensionPixelSize(android.R.dimen.notification_large_icon_height);
largeIcon = Bitmap.createScaledBitmap(largeIcon, largeIconWidth, largeIconHeight, false);
return new NotificationCompat.Builder(context, ApplicationConstants.MEDIA_NOTIFICATION_CHANNEL_ID).setSmallIcon(R.drawable.ic_notification_logo).setLargeIcon(largeIcon).addAction(new NotificationCompat.Action(R.drawable.ic_notification_play_pause, "Play/Pause", playPauseIntent)).setStyle(new MediaStyle().setMediaSession(mediaSessionCompat.getSessionToken()).setShowActionsInCompactView(0)).setColorized(true).setContentTitle(title).setContentText(description).setContentIntent(openPartyIntent).setOngoing(true).setDefaults(4).build();
}
use of android.support.v4.media.session.MediaSessionCompat in project zype-android by zype.
the class PlayerFragment method initMediaSession.
// Media session
private void initMediaSession() {
ComponentName mediaButtonReceiver = new ComponentName(getContext().getApplicationContext(), RemoteControlReceiver.class);
mediaSession = new MediaSessionCompat(getContext().getApplicationContext(), "TAG_MEDIA_SESSION", mediaButtonReceiver, null);
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
mediaButtonIntent.setClass(getActivity(), RemoteControlReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(getActivity(), 0, mediaButtonIntent, 0);
mediaSession.setMediaButtonReceiver(pendingIntent);
PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder().setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE);
mediaSession.setPlaybackState(stateBuilder.build());
mediaSession.setCallback(new MediaSessionCompat.Callback() {
@Override
public void onPlay() {
super.onPlay();
}
@Override
public void onPause() {
super.onPause();
}
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
super.onPlayFromMediaId(mediaId, extras);
}
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
return super.onMediaButtonEvent(mediaButtonEvent);
}
});
MediaControllerCompat mediaController = new MediaControllerCompat(getActivity(), mediaSession);
MediaControllerCompat.setMediaController(getActivity(), mediaController);
}
Aggregations