Search in sources :

Example 31 with MediaSessionCompat

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);
}
Also used : MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 32 with MediaSessionCompat

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);
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 33 with MediaSessionCompat

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));
}
Also used : IntentFilter(android.content.IntentFilter) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PlaybackServiceEvent(de.danoeh.antennapod.event.playback.PlaybackServiceEvent) CastStateListener(de.danoeh.antennapod.playback.cast.CastStateListener) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 34 with MediaSessionCompat

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();
}
Also used : MediaStyle(android.support.v4.media.app.NotificationCompat.MediaStyle) NotificationCompat(android.support.v4.app.NotificationCompat) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PartyActivity(se.zinokader.spotiq.feature.party.PartyActivity) PendingIntent(android.app.PendingIntent)

Example 35 with MediaSessionCompat

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);
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) Bundle(android.os.Bundle) ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Aggregations

MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)31 Intent (android.content.Intent)17 PendingIntent (android.app.PendingIntent)16 ComponentName (android.content.ComponentName)10 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)8 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)5 Bundle (android.os.Bundle)4 Context (android.content.Context)3 NotificationCompat (android.support.v4.app.NotificationCompat)3 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)3 KeyEvent (android.view.KeyEvent)3 SuppressLint (android.annotation.SuppressLint)2 NotificationManager (android.app.NotificationManager)2 IntentFilter (android.content.IntentFilter)2 Bitmap (android.graphics.Bitmap)2 NonNull (android.support.annotation.NonNull)2 Builder (com.google.android.gms.cast.Cast.CastOptions.Builder)2 NotificationChannel (android.app.NotificationChannel)1 SharedPreferences (android.content.SharedPreferences)1 Canvas (android.graphics.Canvas)1