Java Code Examples for android.support.v4.media.session.MediaSessionCompat#setFlags()

The following examples show how to use android.support.v4.media.session.MediaSessionCompat#setFlags() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MediaNotificationManager.java    From AndroidChromium with Apache License 2.0 6 votes vote down vote up
private MediaSessionCompat createMediaSession() {
    MediaSessionCompat mediaSession = new MediaSessionCompat(
            mContext,
            mContext.getString(R.string.app_name),
            new ComponentName(mContext.getPackageName(),
                    getButtonReceiverClassName()),
            null);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
            | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setCallback(mMediaSessionCallback);

    // TODO(mlamouri): the following code is to work around a bug that hopefully
    // MediaSessionCompat will handle directly. see b/24051980.
    try {
        mediaSession.setActive(true);
    } catch (NullPointerException e) {
        // Some versions of KitKat do not support AudioManager.registerMediaButtonIntent
        // with a PendingIntent. They will throw a NullPointerException, in which case
        // they should be able to activate a MediaSessionCompat with only transport
        // controls.
        mediaSession.setActive(false);
        mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mediaSession.setActive(true);
    }
    return mediaSession;
}
 
Example 2
Source File: MusicService.java    From android-MediaBrowserService with Apache License 2.0 6 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    // Create a new MediaSession.
    mSession = new MediaSessionCompat(this, "MusicService");
    mCallback = new MediaSessionCallback();
    mSession.setCallback(mCallback);
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS |
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    setSessionToken(mSession.getSessionToken());

    mMediaNotificationManager = new MediaNotificationManager(this);

    mPlayback = new MediaPlayerAdapter(this, new MediaPlayerListener());
    Log.d(TAG, "onCreate: MusicService creating MediaSession, and MediaNotificationManager");
}
 
Example 3
Source File: MediaNotificationManager.java    From delion with Apache License 2.0 6 votes vote down vote up
private MediaSessionCompat createMediaSession() {
    MediaSessionCompat mediaSession = new MediaSessionCompat(
            mContext,
            mContext.getString(R.string.app_name),
            new ComponentName(mContext.getPackageName(),
                    getButtonReceiverClassName()),
            null);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
            | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setCallback(mMediaSessionCallback);

    // TODO(mlamouri): the following code is to work around a bug that hopefully
    // MediaSessionCompat will handle directly. see b/24051980.
    try {
        mediaSession.setActive(true);
    } catch (NullPointerException e) {
        // Some versions of KitKat do not support AudioManager.registerMediaButtonIntent
        // with a PendingIntent. They will throw a NullPointerException, in which case
        // they should be able to activate a MediaSessionCompat with only transport
        // controls.
        mediaSession.setActive(false);
        mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
        mediaSession.setActive(true);
    }
    return mediaSession;
}
 
Example 4
Source File: MediaSessionPlaybackActivity.java    From android-PictureInPicture with Apache License 2.0 6 votes vote down vote up
private void initializeMediaSession() {
    mSession = new MediaSessionCompat(this, TAG);
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                    | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mSession.setActive(true);
    MediaControllerCompat.setMediaController(this, mSession.getController());

    MediaMetadataCompat metadata = new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, mMovieView.getTitle())
            .build();
    mSession.setMetadata(metadata);

    MediaSessionCallback mMediaSessionCallback = new MediaSessionCallback(mMovieView);
    mSession.setCallback(mMediaSessionCallback);

    int state =
            mMovieView.isPlaying()
                    ? PlaybackStateCompat.STATE_PLAYING
                    : PlaybackStateCompat.STATE_PAUSED;
    updatePlaybackState(
            state,
            MEDIA_ACTIONS_ALL,
            mMovieView.getCurrentPosition(),
            mMovieView.getVideoResourceId());
}
 
Example 5
Source File: AudioBookPlayService.java    From HaoReader with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 初始化MediaSession
 */
private void initMediaSession() {
    ComponentName mComponent = new ComponentName(getPackageName(), MediaButtonIntentReceiver.class.getName());
    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mComponent);
    PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(this, 0,
            mediaButtonIntent, PendingIntent.FLAG_CANCEL_CURRENT);

    mediaSessionCompat = new MediaSessionCompat(this, TAG, mComponent, mediaButtonReceiverPendingIntent);
    mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
            | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    mediaSessionCompat.setCallback(new MediaSessionCompat.Callback() {
        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            return MediaButtonIntentReceiver.handleIntent(mediaButtonEvent);
        }
    });
    mediaSessionCompat.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
    mediaSessionCompat.setActive(true);
}
 
Example 6
Source File: MainActivity.java    From Android-9-Development-Cookbook with MIT License 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MediaSessionCompat mediaSession =
            new MediaSessionCompat(this, getApplication().getPackageName());
    mediaSession.setCallback(mMediaSessionCallback);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    mediaSession.setActive(true);
    PlaybackStateCompat state = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY |
                    PlaybackStateCompat.ACTION_PLAY_PAUSE |
                    PlaybackStateCompat.ACTION_PAUSE |
                    PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                    PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS).build();
    mediaSession.setPlaybackState(state);
}
 
Example 7
Source File: MusicService.java    From VinylMusicPlayer with GNU General Public License v3.0 6 votes vote down vote up
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);
    mMediaSessionCallback = new MediaSessionCallback(this, getApplicationContext());
    mediaSession = new MediaSessionCompat(this, "VinylMusicPlayer", mediaButtonReceiverComponentName, mediaButtonReceiverPendingIntent);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
            | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    mediaSession.setCallback(mMediaSessionCallback);
    mediaSession.setActive(true);
    mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
    setSessionToken(mediaSession.getSessionToken());
}
 
Example 8
Source File: MediaSessionFactoryImpl.java    From PainlessMusicPlayer with Apache License 2.0 5 votes vote down vote up
@NonNull
@Override
public MediaSessionCompat newMediaSession() {
    final MediaSessionCompat mediaSession = new MediaSessionCompat(
            context, TAG_MEDIA_SESSION, mediaButtonReceiver, mediaButtonIntent);
    mediaSession.setCallback(mediaSessionCallback);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setSessionActivity(PendingIntent.getActivity(context, 1,
            new Intent(context, sessionActivityClass), PendingIntent.FLAG_UPDATE_CURRENT));
    mediaSession.setActive(true);
    return mediaSession;
}
 
Example 9
Source File: BackgroundAudioService.java    From CodenameOne with GNU General Public License v2.0 5 votes vote down vote up
private void initMediaSession() {
    ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
    mMediaSessionCompat = new MediaSessionCompat(getApplicationContext(), "Tag", mediaButtonReceiver, null);

    mMediaSessionCompat.setCallback(mMediaSessionCallback);
    mMediaSessionCompat.setFlags( MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS );

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setClass(this, MediaButtonReceiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
    mMediaSessionCompat.setMediaButtonReceiver(pendingIntent);

    setSessionToken(mMediaSessionCompat.getSessionToken());
}
 
Example 10
Source File: RemoteControlClientLP.java    From Popeens-DSub with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void register(Context context, ComponentName mediaButtonReceiverComponent) {
	downloadService = (DownloadService) context;
	mediaSession = new MediaSessionCompat(downloadService, "DSub MediaSession");

	Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
	mediaButtonIntent.setComponent(mediaButtonReceiverComponent);
	PendingIntent mediaPendingIntent = PendingIntent.getBroadcast(context.getApplicationContext(), 0, mediaButtonIntent, 0);
	mediaSession.setMediaButtonReceiver(mediaPendingIntent);

	Intent activityIntent = new Intent(context, SubsonicFragmentActivity.class);
	activityIntent.putExtra(Constants.INTENT_EXTRA_NAME_DOWNLOAD, true);
	activityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
	PendingIntent activityPendingIntent = PendingIntent.getActivity(context, 0, activityIntent, 0);
	mediaSession.setSessionActivity(activityPendingIntent);

	mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
	mediaSession.setCallback(new EventCallback());

	mediaSession.setPlaybackToLocal(AudioManager.STREAM_MUSIC);
	mediaSession.setActive(true);

	Bundle sessionExtras = new Bundle();
	sessionExtras.putBoolean(WEAR_BACKGROUND_THEME, true);
	sessionExtras.putBoolean(WEAR_RESERVE_SKIP_TO_PREVIOUS, true);
	sessionExtras.putBoolean(WEAR_RESERVE_SKIP_TO_NEXT, true);
	sessionExtras.putBoolean(AUTO_RESERVE_SKIP_TO_PREVIOUS, true);
	sessionExtras.putBoolean(AUTO_RESERVE_SKIP_TO_NEXT, true);
	mediaSession.setExtras(sessionExtras);

	imageLoader = SubsonicActivity.getStaticImageLoader(context);
}
 
Example 11
Source File: MyMediaBrowserService.java    From carstream-android-auto with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    mediaSessionCompat = new MediaSessionCompat(this, "youtube");
    mediaSessionCompat.setCallback(new MediaCallBack());
    mediaSessionCompat.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                    MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    setSessionToken(mediaSessionCompat.getSessionToken());
    setup();
}
 
Example 12
Source File: PlaybackFragment.java    From leanback-assistant with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int movieId = getActivity().getIntent().getIntExtra(EXTRA_MOVIE_ID, -1);
    if (movieId == -1) {
        Log.w(TAG, "Invalid movieId, cannot playback.");
        throw new IllegalArgumentException("Invalid movieId " + movieId);
    }

    mPlaylistAdapter =
            MockPlaylistAdapterFactory.createMoviePlaylistAdapterWithActiveMovieId(movieId);

    mSession = new MediaSessionCompat(getContext(), TAG);
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                    | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mSession.setActive(true);
    MediaControllerCompat.setMediaController((Activity) getContext(), mSession.getController());

    mPlayerGlue =
            new PrimaryPlaybackControlsGlue<>(
                    getContext(),
                    new MediaPlayerAdapter(getContext()),
                    mSession.getController());
    mPlayerGlue.setHost(new VideoSupportFragmentGlueHost(this));
    mPlayerGlue.addPlayerCallback(playWhenReadyPlayerCallback);
    mPlayerGlue.addPlayerCallback(playPausePlayerCallback);

    mMediaSessionCallback = new MediaSessionCallback(mPlayerGlue);
    mSession.setCallback(mMediaSessionCallback);

    playMedia(mPlaylistAdapter.getCurrentItem());
}
 
Example 13
Source File: PlayManager.java    From BeMusic with Apache License 2.0 5 votes vote down vote up
private void startRemoteControl() {
    ComponentName mediaButtonReceiver = new ComponentName(mContext, RemoteControlReceiver.class);
    mMediaSessionCompat = new MediaSessionCompat(mContext, TAG, mediaButtonReceiver, null);
    mMediaSessionCompat.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
    );
    mMediaSessionCompat.setCallback(mSessionCallback);
    setSessionActive(true);
    changeMediaSessionState(PlayService.STATE_IDLE);
}
 
Example 14
Source File: MediaSessionManager.java    From react-native-jw-media-player with MIT License 5 votes vote down vote up
/**
 * Initializes a new MediaSessionManager.
 *
 * @param context
 * @param playerView
 */
public MediaSessionManager(Context context,
                              JWPlayerView playerView,
                              NotificationWrapper notificationWrapper) {
	mPlayer = playerView;
	mNotificationWrapper = notificationWrapper;

	// Create a new MediaSession
	mMediaSessionCompat = new MediaSessionCompat(context,
												 MediaSessionManager.class.getSimpleName());
	mMediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
										 | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
	mMediaSessionCompat.setCallback(new MediaSessionCallback(mPlayer));

	// Register listeners.
	mPlayer.addOnPlayListener(this);
	mPlayer.addOnPauseListener(this);
	mPlayer.addOnBufferListener(this);
	mPlayer.addOnErrorListener(this);
	mPlayer.addOnPlaylistListener(this);
	mPlayer.addOnPlaylistItemListener(this);
	mPlayer.addOnPlaylistCompleteListener(this);
	mPlayer.addOnAdPlayListener(this);
	mPlayer.addOnErrorListener(this);
	mPlayer.addOnAdSkippedListener(this);
	mPlayer.addOnAdCompleteListener(this);
}
 
Example 15
Source File: PlaybackServiceStatusHelper.java    From odyssey with GNU General Public License v3.0 5 votes vote down vote up
public PlaybackServiceStatusHelper(PlaybackService playbackService) {
    mPlaybackService = playbackService;

    // Get MediaSession objects
    mMediaSession = new MediaSessionCompat(mPlaybackService, "OdysseyPBS");

    // Register the callback for the MediaSession
    mMediaSession.setCallback(new OdysseyMediaSessionCallback());

    mCoverLoader = new CoverBitmapLoader(mPlaybackService, new BitmapCoverReceiver());

    // Register the button receiver
    PendingIntent mediaButtonPendingIntent = PendingIntent.getBroadcast(mPlaybackService, 0, new Intent(mPlaybackService, RemoteControlReceiver.class), PendingIntent.FLAG_UPDATE_CURRENT);
    mMediaSession.setMediaButtonReceiver(mediaButtonPendingIntent);
    mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS + MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    // Initialize the notification manager
    mNotificationManager = new OdysseyNotificationManager(mPlaybackService);

    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(playbackService);

    mHideArtwork = sharedPref.getBoolean(playbackService.getString(R.string.pref_hide_artwork_key), playbackService.getResources().getBoolean(R.bool.pref_hide_artwork_default));

    hideMediaOnLockscreen(sharedPref.getBoolean(playbackService.getString(R.string.pref_hide_media_on_lockscreen_key), playbackService.getResources().getBoolean(R.bool.pref_hide_media_on_lockscreen_default)));

    Intent settingChangedIntent = new Intent(MESSAGE_HIDE_ARTWORK_CHANGED);
    settingChangedIntent.putExtra(MESSAGE_EXTRA_HIDE_ARTWORK_CHANGED_VALUE, mHideArtwork);
    mPlaybackService.sendBroadcast(settingChangedIntent);
}
 
Example 16
Source File: MusicService.java    From NewFastFrame with Apache License 2.0 4 votes vote down vote up
private void setUpMediaSession() {
    mediaSessionCompat = new MediaSessionCompat(this, "listener");
    mediaSessionCompat.setCallback(new MediaSessionCompat.Callback() {
        @Override
        public void onPlay() {
            CommonLogger.e("mediaSession:onPlay");
            mMusicBinder.play(0);

        }

        @Override
        public void onPause() {
            CommonLogger.e("mediaSession:onPause");
            mMusicBinder.pause();
        }

        @Override
        public void onSkipToNext() {
            CommonLogger.e("mediaSession:onSkipToNext");
            mMusicBinder.next();
        }

        @Override
        public void onSkipToPrevious() {
            CommonLogger.e("mediaSession:onSkipToPrevious");
            mMusicBinder.pre();
        }

        @Override
        public void onStop() {
            mMusicBinder.pause();
        }

        @Override
        public void onSeekTo(long pos) {
            CommonLogger.e("mediaSession:onSeekTo");
        }
    });
    mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSessionCompat.setActive(true);
}
 
Example 17
Source File: MusicService.java    From MusicPlayer with GNU General Public License v3.0 4 votes vote down vote up
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, "MusicR", 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(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
            | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);

    mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
 
Example 18
Source File: MediaSessionManager.java    From YCAudioPlayer with Apache License 2.0 4 votes vote down vote up
private void setupMediaSession() {
    mMediaSession = new MediaSessionCompat(mPlayService, TAG);
    mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);
    mMediaSession.setCallback(callback);
    mMediaSession.setActive(true);
}
 
Example 19
Source File: RadioPlayerService.java    From monkeyboard-radio-android with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void onCreate() {
    // Get preferences storage;
    sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    // Get audio manager
    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    if (!sharedPreferences.getBoolean(getString(R.string.SYNC_VOLUME_KEY), true)) {
        volume = sharedPreferences.getInt(getString(R.string.VOLUME_KEY), 13);
    } else {
        // Set the player volume to the system volume
        volume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
    }

    Log.d(TAG, "Start volume: " + volume);

    // Build a media session for the RadioPlayer
    mediaSession = new MediaSessionCompat(this, this.getClass().getSimpleName());
    mediaSession.setCallback(new MediaSessionCallback());
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    setupVolumeControls();

    // Build a playback state for the player
    playbackStateBuilder = new PlaybackStateCompat.Builder();

    // Update info of the current session to initial state
    updatePlaybackState(PlaybackStateCompat.STATE_STOPPED);
    createNewPlaybackMetadataForStation(new RadioStation());

    // Connect to the radio API
    radio = new RadioDevice(getApplicationContext());

    // Register listeners of the radio
    radio.getListenerManager().registerDataListener(dataListener);
    radio.getListenerManager().registerConnectionStateChangedListener(connectionStateListener);

    saveVolume(volume);

    loadPreferences();

    // Start the process of connecting to the radio device
    openConnection();
}
 
Example 20
Source File: MusicService.java    From LyricHere with Apache License 2.0 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();
    LogUtils.d(TAG, "onCreate");

    mPlayingQueue = new ArrayList<>();
    mMusicProvider = new MusicProvider();

    mEventReceiver = new ComponentName(getPackageName(), MediaButtonReceiver.class.getName());

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mEventReceiver);
    mMediaPendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);

    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService", mEventReceiver, mMediaPendingIntent);

    final MediaSessionCallback cb = new MediaSessionCallback();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        // Shouldn't really have to do this but the MediaSessionCompat method uses
        // an internal proxy class, which doesn't forward events such as
        // onPlayFromMediaId when running on Lollipop.
        final MediaSession session = (MediaSession) mSession.getMediaSession();
        session.setCallback(new MediaSessionCallbackProxy(cb));
    } else {
        mSession.setCallback(cb);
    }

    setSessionToken(mSession.getSessionToken());
    mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    mPlayback = new LocalPlayback(this, mMusicProvider);
    mPlayback.setState(PlaybackStateCompat.STATE_NONE);
    mPlayback.setCallback(this);
    mPlayback.start();

    Context context = getApplicationContext();
    Intent intent = new Intent(context, MusicPlayerActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/,
            intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setSessionActivity(pi);

    mSessionExtras = new Bundle();
    mSession.setExtras(mSessionExtras);

    updatePlaybackState(null);

    mMediaNotificationManager = new MediaNotificationManager(this);
}