Search in sources :

Example 6 with MediaSessionCompat

use of android.support.v4.media.session.MediaSessionCompat in project AndroidAudioExample by SergeyVinyar.

the class MediaStyleHelper method from.

/**
 * Build a notification using the information from the given media session. Makes heavy use
 * of {@link MediaMetadataCompat#getDescription()} to extract the appropriate information.
 *
 * @param context      Context used to construct the notification.
 * @param mediaSession Media session to get information.
 * @return A pre-built notification with information from the given media session.
 */
static NotificationCompat.Builder from(Context context, MediaSessionCompat mediaSession) {
    MediaControllerCompat controller = mediaSession.getController();
    MediaMetadataCompat mediaMetadata = controller.getMetadata();
    MediaDescriptionCompat description = mediaMetadata.getDescription();
    NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle(description.getTitle()).setContentText(description.getSubtitle()).setSubText(description.getDescription()).setLargeIcon(description.getIconBitmap()).setContentIntent(controller.getSessionActivity()).setDeleteIntent(MediaButtonReceiver.buildMediaButtonPendingIntent(context, PlaybackStateCompat.ACTION_STOP)).setVisibility(NotificationCompat.VISIBILITY_PUBLIC);
    return builder;
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) NotificationCompat(android.support.v7.app.NotificationCompat) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat) MediaDescriptionCompat(android.support.v4.media.MediaDescriptionCompat)

Example 7 with MediaSessionCompat

use of android.support.v4.media.session.MediaSessionCompat in project android-app by LISTEN-moe.

the class RadioService method initMediaSession.

private void initMediaSession() {
    mediaSession = new MediaSessionCompat(this, APP_PACKAGE_NAME, null, null);
    mediaSession.setRatingType(RatingCompat.RATING_HEART);
    mediaSession.setCallback(new MediaSessionCompat.Callback() {

        @Override
        public void onPlay() {
            play();
        }

        @Override
        public void onPause() {
            pause();
        }

        @Override
        public void onStop() {
            stop();
        }

        @Override
        public void onSkipToNext() {
        }

        @Override
        public void onSkipToPrevious() {
        }

        @Override
        public void onSeekTo(long pos) {
        }

        @Override
        public void onSetRating(RatingCompat rating) {
            favoriteCurrentSong();
        }

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            return handleIntent(mediaButtonEvent);
        }

        @Override
        public void onCustomAction(@NonNull String action, Bundle extras) {
            switch(action) {
                case TOGGLE_FAVORITE:
                    favoriteCurrentSong();
                    updateMediaSessionPlaybackState();
                    break;
                default:
                    Log.d(TAG, "Unsupported action: " + action);
                    break;
            }
        }

        @Override
        public void onPlayFromSearch(String query, Bundle extras) {
            // We don't support searching for specific things since it's just a radio stream
            // so just toggle playback
            togglePlayPause();
        }
    });
    mediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
    mediaSession.setActive(true);
}
Also used : RatingCompat(android.support.v4.media.RatingCompat) Bundle(android.os.Bundle) Intent(android.content.Intent) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 8 with MediaSessionCompat

use of android.support.v4.media.session.MediaSessionCompat in project butter-android by butterproject.

the class TVAbsPlayerFragment method onCreate.

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mediaSession = new MediaSessionCompat(requireContext(), BuildConfig.APPLICATION_ID);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setMediaButtonReceiver(null);
    mediaSession.setCallback(new PlayerSessionCallback());
    stateBuilder = new PlaybackStateCompat.Builder();
    metadataBuilder = new MediaMetadataCompat.Builder();
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 9 with MediaSessionCompat

use of android.support.v4.media.session.MediaSessionCompat in project vialer-android by VoIPGRID.

the class BluetoothMediaSessionService method onCreate.

@Override
public void onCreate() {
    mContext = this;
    mRemoteLogger = new RemoteLogger(BluetoothMediaSessionService.class).enableConsoleLogging();
    mRemoteLogger.v("onCreate()");
    MediaSessionCompat session = new MediaSessionCompat(this, TAG);
    PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder();
    stateBuilder.setActions(PlaybackStateCompat.ACTION_PLAY | PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_PLAY_FROM_MEDIA_ID | PlaybackStateCompat.ACTION_PAUSE | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS);
    stateBuilder.setState(PlaybackStateCompat.STATE_PLAYING, 0, 1);
    session.setPlaybackState(stateBuilder.build());
    session.setCallback(mSessionCallback);
    session.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    session.setPlaybackToLocal(AudioManager.STREAM_VOICE_CALL);
    mSession = session;
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) RemoteLogger(com.voipgrid.vialer.logging.RemoteLogger) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 10 with MediaSessionCompat

use of android.support.v4.media.session.MediaSessionCompat in project LibreraReader by foobnix.

the class TTSService method onCreate.

@Override
public void onCreate() {
    LOG.d(TAG, "Create");
    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "TTSService");
    AppState.get().load(getApplicationContext());
    mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    mAudioManager.requestAudioFocus(listener, AudioManager.STREAM_VOICE_CALL, AudioManager.AUDIOFOCUS_GAIN);
    mMediaSessionCompat = new MediaSessionCompat(getApplicationContext(), "Tag");
    mMediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mMediaSessionCompat.setCallback(new MediaSessionCompat.Callback() {

        @Override
        public boolean onMediaButtonEvent(Intent intent) {
            LOG.d(TAG, "onMediaButtonEvent", isActivated, intent);
            if (isActivated) {
                KeyEvent event = (KeyEvent) intent.getExtras().get(Intent.EXTRA_KEY_EVENT);
                if (KeyEvent.ACTION_UP == event.getAction() && KeyEvent.KEYCODE_HEADSETHOOK == event.getKeyCode()) {
                    LOG.d(TAG, "onStartStop", "KEYCODE_HEADSETHOOK");
                    boolean isPlaying = TTSEngine.get().isPlaying();
                    if (isPlaying) {
                        TTSEngine.get().stop();
                    } else {
                        playPage("", AppState.get().lastBookPage, null);
                    }
                }
            }
            return isActivated;
        }
    });
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setClass(this, MediaButtonReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
    mMediaSessionCompat.setMediaButtonReceiver(pendingIntent);
    TTSEngine.get().getTTS();
}
Also used : PowerManager(android.os.PowerManager) KeyEvent(android.view.KeyEvent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) PendingIntent(android.app.PendingIntent) 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