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

The following examples show how to use android.support.v4.media.session.MediaSessionCompat#setActive() . 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 365browser with Apache License 2.0 6 votes vote down vote up
private MediaSessionCompat createMediaSession() {
    Context context = getContext();
    MediaSessionCompat mediaSession =
            new MediaSessionCompat(context, context.getString(R.string.app_name),
                    new ComponentName(context, getButtonReceiverClass()), 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: StreamFragment.java    From Twire with GNU General Public License v3.0 6 votes vote down vote up
private void initializePlayer() {
    if (player == null) {
        player = new SimpleExoPlayer.Builder(getContext()).build();
        player.addListener(this);
        mVideoView.setPlayer(player);
        mVideoView.setPlaybackPreparer(this);

        if (currentMediaSource != null)
            player.prepare(currentMediaSource);

        mediaSession = new MediaSessionCompat(getContext(), getContext().getPackageName());
        MediaSessionConnector mediaSessionConnector = new MediaSessionConnector(mediaSession);
        mediaSessionConnector.setPlayer(player);
        mediaSession.setActive(true);

        progressHandler.postDelayed(progressRunnable, 1000);
    }
}
 
Example 3
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 4
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 5
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 6
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 7
Source File: MediaSessionPlaybackActivity.java    From media-samples 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 8
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 9
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 10
Source File: VideoFragment.java    From leanback-homescreen-channels with Apache License 2.0 5 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mMediaPlayerGlue = new PlaybackBannerControlGlue<MediaPlayerAdapter>(getContext(),
            new int[]{1}, new MediaPlayerAdapter(getContext())) {
        @Override
        public long getSupportedActions() {
            return PlaybackBannerControlGlue.ACTION_PLAY_PAUSE
                    | PlaybackBannerControlGlue.ACTION_SKIP_TO_NEXT
                    | PlaybackBannerControlGlue.ACTION_SKIP_TO_PREVIOUS;
        }
    };
    mMediaPlayerGlue.setHost(mHost);

    Bundle args = getArguments();
    mSelectedClip = args.getParcelable(PlaybackActivity.EXTRA_CLIP);
    mProgress = args.getLong(PlaybackActivity.EXTRA_PROGRESS);

    mMediaPlayerGlue.setTitle(mSelectedClip.getTitle());
    mMediaPlayerGlue.setSubtitle(mSelectedClip.getDescription());
    mMediaPlayerGlue.getPlayerAdapter().setDataSource(Uri.parse(mSelectedClip.getVideoUrl()));
    mSession = new MediaSessionCompat(getContext(), "TvLauncherSampleApp");
    mSession.setActive(true);
    mSession.setCallback(new MediaSessionCallback());
    playWhenReady();
    updatePlaybackState();
    updateMetadata(mSelectedClip);
}
 
Example 11
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 12
Source File: MusicNotificationManager.java    From vk_music_android with GNU General Public License v3.0 5 votes vote down vote up
private void createMediaSession() {
    ComponentName receiver = new ComponentName(context.getPackageName(), RemoteReceiver.class.getName());
    mediaSession = new MediaSessionCompat(context, "MusicService", receiver, null);
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setPlaybackState(
            new PlaybackStateCompat.Builder()
                    .setState(PlaybackStateCompat.STATE_PLAYING, 0, 0)
                    .setActions(PlaybackStateCompat.ACTION_PLAY_PAUSE)
                    .build()
    );

    AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
    audioManager.requestAudioFocus(focusChange -> {}, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    mediaSession.setActive(true);
}
 
Example 13
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 14
Source File: MainActivity.java    From lbry-android with MIT License 5 votes vote down vote up
public void initMediaSession() {
    ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
    mediaSession = new MediaSessionCompat(getApplicationContext(), "LBRYMediaSession", mediaButtonReceiver, null);
    MediaSessionConnector connector = new MediaSessionConnector(mediaSession);
    connector.setPlayer(MainActivity.appPlayer);
    mediaSession.setActive(true);
}
 
Example 15
Source File: MediaSessionManager.java    From Musicoco with Apache License 2.0 5 votes vote down vote up
/**
 * 初始化并激活MediaSession
 */
private void setupMediaSession() {
    mMediaSession = new MediaSessionCompat(context, TAG);
    mMediaSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                    MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
    );
    mMediaSession.setCallback(callback);
    mMediaSession.setActive(true);
}
 
Example 16
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 17
Source File: Player.java    From zapp with MIT License 4 votes vote down vote up
public Player(Context context) {
	settings = new SettingsRepository(context);
	networkConnectionHelper = new NetworkConnectionHelper(context);

	String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name));
	TransferListener transferListener = new OnlyWifiTransferListener();
	dataSourceFactory = new DefaultDataSourceFactory(context, userAgent, transferListener);
	TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory();
	DefaultTrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);

	player = ExoPlayerFactory.newSimpleInstance(context, trackSelector);
	playerHandler = new Handler(player.getApplicationLooper());
	trackSelectorWrapper = new TrackSelectorWrapper(trackSelector);

	// media session setup
	mediaSession = new MediaSessionCompat(context, context.getPackageName());
	MediaSessionConnector mediaSessionConnector = new MediaSessionConnector(mediaSession);
	mediaSessionConnector.setPlayer(player, null);
	mediaSession.setActive(true);

	// audio focus setup
	AudioAttributes audioAttributes = new AudioAttributes.Builder()
		.setUsage(C.USAGE_MEDIA)
		.setContentType(C.CONTENT_TYPE_MOVIE)
		.build();
	player.setAudioAttributes(audioAttributes, true);

	playerEventHandler = new PlayerEventHandler();

	// enable subtitles
	enableSubtitles(settings.getEnableSubtitles());

	// wakelocks
	playerWakeLocks = new PlayerWakeLocks(context, "Zapp::Player");
	Disposable wakelockDisposable = playerEventHandler
		.getShouldHoldWakelock()
		.distinctUntilChanged()
		.subscribe(this::shouldHoldWakelockChanged);
	disposables.add(wakelockDisposable);

	// set listeners
	networkConnectionHelper.startListenForNetworkChanges(this::setStreamQualityByNetworkType);
}
 
Example 18
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 19
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 20
Source File: MusicService.java    From YTPlayer with GNU General Public License v3.0 4 votes vote down vote up
public static void functionInMainActivity(Context activity) {

       // Toast.makeText(activity, "Function in MainActivity...!", Toast.LENGTH_SHORT).show();

        MusicService.activity = activity.getApplicationContext();

        settingPref = activity.getSharedPreferences("settings",MODE_PRIVATE);
        isEqualizerEnabled = settingPref.getBoolean("equalizer_enabled",false);

        dataSourceFactory = new DefaultDataSourceFactory(activity,
                Util.getUserAgent(activity,
                        activity.getResources().getString(R.string.app_name)), BANDWIDTH_METER);

        player = ExoPlayerFactory.newSimpleInstance(activity, trackSelector);

        createNotification(activity);

        playListItems = new ArrayList<>();
        yturls = new ArrayList<>();
        nPlayModels = new ArrayList<>();

        ComponentName mediaButtonReceiverComponentName = new ComponentName(
                activity,
                SongBroadCast.class);
        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setComponent(mediaButtonReceiverComponentName);
        PendingIntent mediaButtonReceiverPendingIntent = PendingIntent.getBroadcast(
                activity,
                0,
                mediaButtonIntent,
                0);
        mediaSession = new MediaSessionCompat(activity,
                "MusicPlayer",
                mediaButtonReceiverComponentName,
                mediaButtonReceiverPendingIntent);
        mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS
        );
        mediaSession.setCallback(mMediaSessionCallback);
        mediaSession.setActive(true);
        mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);

        preferences = activity.getSharedPreferences("history",MODE_PRIVATE);

    }