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

The following examples show how to use android.support.v4.media.session.MediaSessionCompat#setMediaButtonReceiver() . 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: ReadAloudService.java    From MyBookshelf 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.setCallback(new MediaSessionCompat.Callback() {
        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            return MediaButtonIntentReceiver.handleIntent(ReadAloudService.this, mediaButtonEvent);
        }
    });
    mediaSessionCompat.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
 
Example 2
Source File: ReadAloudService.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);
}
 
Example 3
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 4
Source File: MusicPlayer.java    From Jockey with Apache License 2.0 6 votes vote down vote up
/**
 * Initiate a MediaSession to allow the Android system to interact with the player
 */
private void initMediaSession() {
    Timber.i("Initializing MediaSession");
    ComponentName mbrComponent = new ComponentName(mContext, MediaButtonReceiver.class.getName());
    mMediaSession = new MediaSessionCompat(mContext, TAG, mbrComponent, null);
    mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
            | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    mMediaSession.setCallback(new MediaSessionCallback(this, mMediaBrowserRoot));
    mMediaSession.setSessionActivity(
            PendingIntent.getActivity(
                    mContext, 0,
                    LibraryActivity.newNowPlayingIntent(mContext)
                            .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP),
                    PendingIntent.FLAG_CANCEL_CURRENT));

    updateMediaSession();

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setClass(mContext, MediaButtonReceiver.class);
    PendingIntent mbrIntent = PendingIntent.getBroadcast(mContext, 0, mediaButtonIntent, 0);
    mMediaSession.setMediaButtonReceiver(mbrIntent);

    mMediaSession.setActive(true);
}
 
Example 5
Source File: ReadAloudService.java    From a 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.setCallback(new MediaSessionCompat.Callback() {
        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            return MediaButtonIntentReceiver.handleIntent(ReadAloudService.this, mediaButtonEvent);
        }
    });
    mediaSessionCompat.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
 
Example 6
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 7
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 8
Source File: ServicePlayMusic.java    From Bop with Apache License 2.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_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 9
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 10
Source File: PlayerService.java    From Prodigal with Apache License 2.0 5 votes vote down vote up
private void initMediaSession() {
        ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
        mediaSession = new MediaSessionCompat(getApplicationContext(), "PrdPlayer", mediaButtonReceiver, null);
//        mediaSession.setCallback(mMediaSessionCallback);
        mediaSession.setFlags( 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);
        mediaSession.setMediaButtonReceiver(pendingIntent);
        setSessionToken(mediaSession.getSessionToken());

    }
 
Example 11
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 12
Source File: PlayerService.java    From AndroidAudioExample with MIT License 4 votes vote down vote up
@Override
public void onCreate() {
    super.onCreate();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        @SuppressLint("WrongConstant") NotificationChannel notificationChannel = new NotificationChannel(NOTIFICATION_DEFAULT_CHANNEL_ID, getString(R.string.notification_channel_name), NotificationManagerCompat.IMPORTANCE_DEFAULT);
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);

        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_MEDIA)
                .setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
                .build();
        audioFocusRequest = new AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
                .setOnAudioFocusChangeListener(audioFocusChangeListener)
                .setAcceptsDelayedFocusGain(false)
                .setWillPauseWhenDucked(true)
                .setAudioAttributes(audioAttributes)
                .build();
    }

    audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);

    mediaSession = new MediaSessionCompat(this, "PlayerService");
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setCallback(mediaSessionCallback);

    Context appContext = getApplicationContext();

    Intent activityIntent = new Intent(appContext, MainActivity.class);
    mediaSession.setSessionActivity(PendingIntent.getActivity(appContext, 0, activityIntent, 0));

    Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON, null, appContext, MediaButtonReceiver.class);
    mediaSession.setMediaButtonReceiver(PendingIntent.getBroadcast(appContext, 0, mediaButtonIntent, 0));

    exoPlayer = ExoPlayerFactory.newSimpleInstance(this, new DefaultRenderersFactory(this), new DefaultTrackSelector(), new DefaultLoadControl());
    exoPlayer.addListener(exoPlayerListener);
    DataSource.Factory httpDataSourceFactory = new OkHttpDataSourceFactory(new OkHttpClient(), Util.getUserAgent(this, getString(R.string.app_name)));
    Cache cache = new SimpleCache(new File(this.getCacheDir().getAbsolutePath() + "/exoplayer"), new LeastRecentlyUsedCacheEvictor(1024 * 1024 * 100)); // 100 Mb max
    this.dataSourceFactory = new CacheDataSourceFactory(cache, httpDataSourceFactory, CacheDataSource.FLAG_BLOCK_ON_CACHE | CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR);
    this.extractorsFactory = new DefaultExtractorsFactory();
}
 
Example 13
Source File: MusicService.java    From Phonograph 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, "Phonograph", 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(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS
            | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);

    mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
 
Example 14
Source File: MusicService.java    From RetroMusicPlayer 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, "RetroMusic", 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(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS
            | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);

    mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
 
Example 15
Source File: MusicService.java    From Orin 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, "Phonograph", 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(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS
            | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);

    mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
 
Example 16
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 17
Source File: MusicService.java    From Music-Player 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, getResources().getString(R.string.main_activity_name), 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(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS
            | MediaSession.FLAG_HANDLES_MEDIA_BUTTONS);

    mediaSession.setMediaButtonReceiver(mediaButtonReceiverPendingIntent);
}
 
Example 18
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);

    }