Search in sources :

Example 6 with MediaSession

use of android.media.session.MediaSession in project MusicDNA by harjot-oberai.

the class MediaPlayerService method initMediaSessions.

private void initMediaSessions() {
    if (pFragment != null) {
        pFragment.mCallback7 = this;
        m_objMediaPlayer = pFragment.mMediaPlayer;
        m_objMediaSessionManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
        m_objMediaSession = new MediaSession(getApplicationContext(), "sample session");
        m_objMediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS | MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
        MediaMetadata.Builder metadataBuilder = new MediaMetadata.Builder();
        if (pFragment.localIsPlaying) {
            if (pFragment.localTrack != null) {
                metadataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, pFragment.localTrack.getTitle());
                metadataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, pFragment.localTrack.getArtist());
            }
        } else {
            if (pFragment.track != null) {
                metadataBuilder.putString(MediaMetadata.METADATA_KEY_TITLE, pFragment.track.getTitle());
                metadataBuilder.putString(MediaMetadata.METADATA_KEY_ARTIST, "");
            }
        }
        if (pFragment.selected_track_image != null && pFragment.selected_track_image.getDrawable() != null) {
            if (((BitmapDrawable) pFragment.selected_track_image.getDrawable()).getBitmap() != null) {
                metadataBuilder.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, ((BitmapDrawable) pFragment.selected_track_image.getDrawable()).getBitmap());
            }
        }
        m_objMediaSession.setMetadata(metadataBuilder.build());
        PlaybackState.Builder stateBuilder = new PlaybackState.Builder();
        stateBuilder.setActions(PlaybackState.ACTION_PLAY | PlaybackState.ACTION_PLAY_PAUSE | PlaybackState.ACTION_PAUSE | PlaybackState.ACTION_SKIP_TO_NEXT | PlaybackState.ACTION_SKIP_TO_PREVIOUS);
        try {
            if (pFragment.mMediaPlayer != null) {
                stateBuilder.setState(!pFragment.mMediaPlayer.isPlaying() ? PlaybackState.STATE_PAUSED : PlaybackState.STATE_PLAYING, PlaybackState.PLAYBACK_POSITION_UNKNOWN, 1.0f);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        m_objMediaSession.setPlaybackState(stateBuilder.build());
        m_objMediaSession.setActive(true);
        m_objMediaController = m_objMediaSession.getController();
        m_objMediaSession.setCallback(new MediaSession.Callback() {

            @Override
            public void onPlay() {
                super.onPlay();
                try {
                    Log.d(Constants.LOG_TAG, "onPlay");
                    PlayerFragment pFrag = pFragment;
                    if (pFrag != null) {
                        if (!pFrag.isStart) {
                            pFrag.togglePlayPause();
                        }
                        pFrag.isStart = false;
                        buildNotification(generateAction(R.drawable.ic_pause_notif, "Pause", Constants.ACTION_PAUSE));
                    }
                } catch (Exception e) {
                }
            }

            @Override
            public void onPause() {
                super.onPause();
                try {
                    PlayerFragment pFrag = pFragment;
                    if (pFrag != null) {
                        pFrag.togglePlayPause();
                        buildNotification(generateAction(R.drawable.ic_play_notif, "Play", Constants.ACTION_PLAY));
                    }
                } catch (Exception e) {
                }
            }

            @Override
            public void onSkipToNext() {
                super.onSkipToNext();
                try {
                    if (pFragment != null) {
                        pFragment.onCallbackCalled(2);
                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                buildNotification(generateAction(R.drawable.ic_pause_notif, "Pause", Constants.ACTION_PAUSE));
                            }
                        }, 100);
                    }
                } catch (Exception e) {
                }
            }

            @Override
            public void onSkipToPrevious() {
                super.onSkipToPrevious();
                try {
                    if (pFragment != null) {
                        pFragment.onCallbackCalled(3);
                        final Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {

                            @Override
                            public void run() {
                                buildNotification(generateAction(R.drawable.ic_pause_notif, "Pause", Constants.ACTION_PAUSE));
                            }
                        }, 100);
                    }
                } catch (Exception e) {
                }
            }

            @Override
            public void onFastForward() {
                super.onFastForward();
                Log.d(Constants.LOG_TAG, "onFastForward");
                if (pFragment != null)
                    pFragment.mCallback.onComplete();
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        buildNotification(generateAction(R.drawable.ic_pause_notif, "Pause", Constants.ACTION_PAUSE));
                    }
                }, 100);
            }

            @Override
            public void onRewind() {
                super.onRewind();
                Log.d(Constants.LOG_TAG, "onRewind");
                if (pFragment != null)
                    pFragment.mCallback.onPreviousTrack();
                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        buildNotification(generateAction(R.drawable.ic_pause_notif, "Pause", Constants.ACTION_PAUSE));
                    }
                }, 100);
            }

            @Override
            public void onStop() {
                super.onStop();
            // if (pFragment.mMediaPlayer != null) {
            // if (pFragment.mMediaPlayer.isPlaying()) {
            // buildNotification(generateAction(R.drawable.ic_pause_notif, "Pause", Constants.ACTION_PAUSE));
            // } else {
            // buildNotification(generateAction(R.drawable.ic_play_notif, "Play", Constants.ACTION_PLAY));
            // }
            // }
            }

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

            @Override
            public void onSetRating(Rating rating) {
                super.onSetRating(rating);
            }
        });
    }
}
Also used : Rating(android.media.Rating) Handler(android.os.Handler) MediaSession(android.media.session.MediaSession) MediaMetadata(android.media.MediaMetadata) PlaybackState(android.media.session.PlaybackState) PlayerFragment(com.sdsmdg.harjot.MusicDNA.fragments.PlayerFragment.PlayerFragment)

Example 7 with MediaSession

use of android.media.session.MediaSession in project android_frameworks_base by DirtyUnicorns.

the class BrowserService method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    mSession = new MediaSession(this, "com.example.android.musicservicedemo.BrowserService");
    setSessionToken(mSession.getSessionToken());
}
Also used : MediaSession(android.media.session.MediaSession)

Example 8 with MediaSession

use of android.media.session.MediaSession in project android_frameworks_base by DirtyUnicorns.

the class PlayerSession method createSession.

public void createSession() {
    releaseSession();
    MediaSessionManager man = (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
    Log.d(TAG, "Creating session for package " + mContext.getBasePackageName());
    mSession = new MediaSession(mContext, "OneMedia");
    mSession.setCallback(mCallback);
    mSession.setPlaybackState(mPlaybackState);
    mSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
    mSession.setActive(true);
    updateMetadata();
}
Also used : MediaSession(android.media.session.MediaSession) MediaSessionManager(android.media.session.MediaSessionManager)

Example 9 with MediaSession

use of android.media.session.MediaSession in project android_frameworks_base by ResurrectionRemix.

the class BrowserService method onCreate.

@Override
public void onCreate() {
    super.onCreate();
    mSession = new MediaSession(this, "com.example.android.musicservicedemo.BrowserService");
    setSessionToken(mSession.getSessionToken());
}
Also used : MediaSession(android.media.session.MediaSession)

Example 10 with MediaSession

use of android.media.session.MediaSession in project android_frameworks_base by ResurrectionRemix.

the class PlayerSession method createSession.

public void createSession() {
    releaseSession();
    MediaSessionManager man = (MediaSessionManager) mContext.getSystemService(Context.MEDIA_SESSION_SERVICE);
    Log.d(TAG, "Creating session for package " + mContext.getBasePackageName());
    mSession = new MediaSession(mContext, "OneMedia");
    mSession.setCallback(mCallback);
    mSession.setPlaybackState(mPlaybackState);
    mSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);
    mSession.setActive(true);
    updateMetadata();
}
Also used : MediaSession(android.media.session.MediaSession) MediaSessionManager(android.media.session.MediaSessionManager)

Aggregations

MediaSession (android.media.session.MediaSession)16 MediaController (android.media.session.MediaController)4 MediaSessionManager (android.media.session.MediaSessionManager)4 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 MediaDescription (android.media.MediaDescription)1 MediaMetadata (android.media.MediaMetadata)1 MediaPlayer (android.media.MediaPlayer)1 Rating (android.media.Rating)1 PlaybackState (android.media.session.PlaybackState)1 Handler (android.os.Handler)1 NotificationCompat (android.support.v7.app.NotificationCompat)1 Palette (android.support.v7.graphics.Palette)1 MainActivity (com.freedom.lauzy.ticktockmusic.ui.activity.MainActivity)1 PlayerFragment (com.sdsmdg.harjot.MusicDNA.fragments.PlayerFragment.PlayerFragment)1