Search in sources :

Example 1 with PlaybackServiceEvent

use of de.danoeh.antennapod.event.playback.PlaybackServiceEvent 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)

Aggregations

PendingIntent (android.app.PendingIntent)1 ComponentName (android.content.ComponentName)1 Intent (android.content.Intent)1 IntentFilter (android.content.IntentFilter)1 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)1 PlaybackServiceEvent (de.danoeh.antennapod.event.playback.PlaybackServiceEvent)1 CastStateListener (de.danoeh.antennapod.playback.cast.CastStateListener)1