Search in sources :

Example 21 with MediaSessionCompat

use of android.support.v4.media.session.MediaSessionCompat in project Shuttle by timusus.

the class MusicService method setupMediaSession.

private void setupMediaSession() {
    mediaSession = new MediaSessionCompat(this, "Shuttle", mediaButtonReceiverComponent, null);
    mediaSession.setCallback(new MediaSessionCompat.Callback() {

        @Override
        public void onPause() {
            pause();
            pausedByTransientLossOfFocus = false;
        }

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

        @Override
        public void onSeekTo(long pos) {
            seekTo(pos);
        }

        @Override
        public void onSkipToNext() {
            gotoNext(true);
        }

        @Override
        public void onSkipToPrevious() {
            prev();
        }

        @Override
        public void onStop() {
            pause();
            pausedByTransientLossOfFocus = false;
            releaseServiceUiAndStop();
        }

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            Log.e("MediaButtonReceiver", "OnMediaButtonEvent called");
            MediaButtonIntentReceiver.MediaButtonReceiverHelper.onReceive(MusicService.this, mediaButtonEvent);
            return true;
        }
    });
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    // For some reason, MediaSessionCompat doesn't seem to pass all of the available 'actions' on as
    // transport control flags for the RCC, so we do that manually
    RemoteControlClient remoteControlClient = (RemoteControlClient) mediaSession.getRemoteControlClient();
    if (remoteControlClient != null) {
        remoteControlClient.setTransportControlFlags(RemoteControlClient.FLAG_KEY_MEDIA_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_PLAY | RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE | RemoteControlClient.FLAG_KEY_MEDIA_NEXT | RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS | RemoteControlClient.FLAG_KEY_MEDIA_STOP);
    }
}
Also used : RemoteControlClient(android.media.RemoteControlClient) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 22 with MediaSessionCompat

use of android.support.v4.media.session.MediaSessionCompat in project quran_android by quran.

the class AudioService method onCreate.

@Override
public void onCreate() {
    Timber.i("debug: Creating service");
    handler = new ServiceHandler(this);
    final Context appContext = getApplicationContext();
    ((QuranApplication) appContext).getApplicationComponent().inject(this);
    wifiLock = ((WifiManager) appContext.getSystemService(Context.WIFI_SERVICE)).createWifiLock(WifiManager.WIFI_MODE_FULL, "QuranAudioLock");
    notificationManager = (NotificationManager) appContext.getSystemService(NOTIFICATION_SERVICE);
    // create the Audio Focus Helper, if the Audio Focus feature is available
    audioFocusHelper = new AudioFocusHelper(appContext, this);
    broadcastManager = LocalBroadcastManager.getInstance(appContext);
    noisyAudioStreamReceiver = new NoisyAudioStreamReceiver();
    registerReceiver(noisyAudioStreamReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
    ComponentName receiver = new ComponentName(this, MediaButtonReceiver.class);
    mediaSession = new MediaSessionCompat(appContext, "QuranMediaSession", receiver, null);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setCallback(new MediaSessionCallback());
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        setupNotificationChannel();
    }
    notificationColor = ContextCompat.getColor(this, R.color.audio_notification_color);
    try {
        // for Android Wear, use a 1x1 Bitmap with the notification color
        displayIcon = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(displayIcon);
        canvas.drawColor(notificationColor);
    } catch (OutOfMemoryError oom) {
        Crashlytics.logException(oom);
    }
}
Also used : Context(android.content.Context) IntentFilter(android.content.IntentFilter) AudioFocusHelper(com.quran.labs.androidquran.service.util.AudioFocusHelper) Canvas(android.graphics.Canvas) ComponentName(android.content.ComponentName) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 23 with MediaSessionCompat

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

the class AutoMediaBrowserService method setSessionToken.

private void setSessionToken() {
    final MediaSessionCompat mediaSession = App.getService().getMediaSession();
    setSessionToken(mediaSession.getSessionToken());
}
Also used : MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 24 with MediaSessionCompat

use of android.support.v4.media.session.MediaSessionCompat in project Phonograph by kabouzeid.

the class MusicService method setupMediaSession.

private void setupMediaSession() {
    ComponentName mediaButtonReceiverComponentName = new ComponentName(getApplicationContext(), MediaButtonIntentReceiver.class);
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mediaButtonReceiverComponentName);
    PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(getApplicationContext(), 0, mediaButtonIntent, 0);
    mediaSession = new MediaSessionCompat(this, "Phonograph", mediaButtonReceiverComponentName, mediaButtonReceiverPendingIntent);
    mediaSession.setCallback(new MediaSessionCompat.Callback() {

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

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

        @Override
        public void onSkipToNext() {
            playNextSong(true);
        }

        @Override
        public void onSkipToPrevious() {
            back(true);
        }

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

        @Override
        public void onSeekTo(long pos) {
            seek((int) pos);
        }

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            return MediaButtonIntentReceiver.handleIntent(MusicService.this, mediaButtonEvent);
        }
    });
    mediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
    mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
Also used : ComponentName(android.content.ComponentName) PendingIntent(android.app.PendingIntent) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat)

Example 25 with MediaSessionCompat

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

the class AbsPlayerFragment method onCreate.

@Override
public void onCreate(@Nullable final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    mediaSession = new MediaSessionCompat(getContext(), 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().setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_REWIND | PlaybackStateCompat.ACTION_FAST_FORWARD | PlaybackStateCompat.ACTION_SEEK_TO).addCustomAction(ACTION_SCALE, getString(R.string.scale), R.drawable.ic_av_aspect_ratio);
    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)

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