Example usage for android.content Context MEDIA_SESSION_SERVICE

List of usage examples for android.content Context MEDIA_SESSION_SERVICE

Introduction

In this page you can find the example usage for android.content Context MEDIA_SESSION_SERVICE.

Prototype

String MEDIA_SESSION_SERVICE

To view the source code for android.content Context MEDIA_SESSION_SERVICE.

Click Source Link

Document

Use with #getSystemService(String) to retrieve a android.media.session.MediaSessionManager for managing media Sessions.

Usage

From source file:de.bigboot.qcircleview.cover.CoverFragment.java

@AfterViews
protected void init() {
    Preferences preferences = new Preferences(getActivity());

    FragmentManager fragMan = getChildFragmentManager();
    FragmentTransaction fragTransaction = fragMan.beginTransaction();

    Clock clock = preferences.getActiveClock();
    if (clock == null) {
        clock = Clock.STATIC_CLOCKS[0];
    }/*  ww w  . ja v a2 s  .c om*/
    Fragment fragment;
    if (clock instanceof Clock.StaticClock) {
        Clock.StaticClock staticClock = (Clock.StaticClock) clock;
        fragment = staticClock.getFragment();
    } else {
        fragment = CoverAnalogClockFragment_.builder().clock(clock).build();
    }
    fragTransaction.add(R.id.clock_layout, fragment);
    fragTransaction.commit();

    adapter = new CoverAdapter(getActivity());
    mediaSessionListener = new MediaSessionListener();
    mediaSessionManager = (MediaSessionManager) getActivity().getSystemService(Context.MEDIA_SESSION_SERVICE);
    try {
        List<MediaController> mediaControllers = mediaSessionManager
                .getActiveSessions(new ComponentName(getActivity(), NotificationService_.class));
        adapter.onActiveSessionsChanged(mediaControllers);
        mediaSessionManager.addOnActiveSessionsChangedListener(mediaSessionListener,
                new ComponentName(getActivity(), NotificationService_.class));
    } catch (SecurityException ex) {
        // No notification access
    }

    mViewPager.setAdapter(adapter);
    mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            if (position == adapter.getDefaultFragmentPosition()) {
                currentNotification = null;
                menuSlide.setPanelHeight(getResources().getDimensionPixelSize(R.dimen.menu_panel_height));
            } else {
                currentNotification = adapter.getNotification(position);
                menuSlide.setPanelHeight(0);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
    new SwipeDetector(mViewPager, true).setOnSwipeListener(new SwipeDetector.onSwipeEvent() {
        @Override
        public void SwipeEventDetected(View v, SwipeDetector.SwipeTypeEnum SwipeType) {
            if (SwipeType == SwipeDetector.SwipeTypeEnum.TOP_TO_BOTTOM && menuSlide.getPanelHeight() > 0) {
                menuSlide.setPanelState(SlidingUpPanelLayout.PanelState.EXPANDED);
            }
        }
    });
}

From source file:leoisasmendi.android.com.suricatepodcast.services.MediaPlayerService.java

private void initMediaSession() throws RemoteException {
    if (mediaSessionManager != null)
        return; //mediaSessionManager exists

    mediaSessionManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
    // Create a new MediaSession
    mediaSession = new MediaSessionCompat(getApplicationContext(), "AudioPlayer");
    //Get MediaSessions transport controls
    transportControls = mediaSession.getController().getTransportControls();
    //set MediaSession -> ready to receive media commands
    mediaSession.setActive(true);/*from  w  w  w  .j a va 2 s .  com*/
    //indicate that the MediaSession handles transport control commands
    // through its MediaSessionCompat.Callback.
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    //Set mediaSession's MetaData
    updateMetaData();

    // Attach Callback to receive MediaSession updates
    mediaSession.setCallback(new MediaSessionCompat.Callback() {
        // Implement callbacks
        @Override
        public void onPlay() {
            super.onPlay();
            resumeMedia();
            publishStatus(STATUS_PLAYING);
            buildNotification(PlaybackStatus.PLAYING);
        }

        @Override
        public void onPause() {
            super.onPause();
            pauseMedia();
            publishStatus(STATUS_PAUSED);
            buildNotification(PlaybackStatus.PAUSED);
        }

        @Override
        public void onSkipToNext() {
            super.onSkipToNext();
            skipToNext();
            updateMetaData();
            buildNotification(PlaybackStatus.PLAYING);
        }

        @Override
        public void onSkipToPrevious() {
            super.onSkipToPrevious();
            skipToPrevious();
            updateMetaData();
            buildNotification(PlaybackStatus.PLAYING);
        }

        @Override
        public void onStop() {
            super.onStop();
            publishStatus(STATUS_STOPED);
            removeNotification();
            //Stop the service
            stopSelf();
        }

    });
}