Java Code Examples for android.support.v4.media.session.MediaSessionCompat#setCallback()
The following examples show how to use
android.support.v4.media.session.MediaSessionCompat#setCallback() .
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 |
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: MediaNotificationManager.java From delion with Apache License 2.0 | 6 votes |
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 3
Source File: CumulusBrowseService.java From CumulusTV with MIT License | 6 votes |
@Override public void onCreate() { super.onCreate(); mSession = new MediaSessionCompat(this, "session tag"); setSessionToken(mSession.getSessionToken()); mSession.setCallback(new CustomMediaSession(getApplicationContext(), new CustomMediaSession.Callback() { @Override public void onPlaybackStarted(JsonChannel channel) { showNotification(channel); } @Override public void onPlaybackEnded() { stopForeground(true); removeNotification(); } })); mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); }
Example 4
Source File: MusicService.java From VinylMusicPlayer with GNU General Public License v3.0 | 6 votes |
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 5
Source File: ReadAloudService.java From HaoReader with GNU General Public License v3.0 | 6 votes |
/** * 初始化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 6
Source File: MediaSessionWrapper.java From Cheerleader with Apache License 2.0 | 6 votes |
/** * Wrapper used to encapsulate {@link android.support.v4.media.session.MediaSessionCompat} behaviour * as well as a remote control client for lock screen on pre Lollipop. * * @param context holding context. * @param callback callback used to catch media session or lock screen events. * @param audioManager audio manager used to request the focus. */ public MediaSessionWrapper(Context context, MediaSessionWrapperCallback callback, AudioManager audioManager) { mContext = context; mCallback = callback; mAudioManager = audioManager; mRuntimePackageName = context.getPackageName(); initLockScreenRemoteControlClient(context); initPlaybackStateBuilder(); mMediaSession = new MediaSessionCompat(context, TAG); mMediaSession.setCallback(new MediaSessionCallback()); mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); }
Example 7
Source File: RemoteControlClientLP.java From Popeens-DSub with GNU General Public License v3.0 | 5 votes |
@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 8
Source File: MediaSessionManager.java From react-native-jw-media-player with MIT License | 5 votes |
/** * 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 9
Source File: MusicService.java From Muzesto with GNU General Public License v3.0 | 5 votes |
private void setUpMediaSession() { mSession = new MediaSessionCompat(this, "Timber"); mSession.setCallback(new MediaSessionCompat.Callback() { @Override public void onPause() { pause(); mPausedByTransientLossOfFocus = false; } @Override public void onPlay() { play(); } @Override public void onSeekTo(long pos) { seek(pos); } @Override public void onSkipToNext() { gotoNext(true); } @Override public void onSkipToPrevious() { prev(false); } @Override public void onStop() { pause(); mPausedByTransientLossOfFocus = false; seek(0); releaseServiceUiAndStop(); } }); mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS); }
Example 10
Source File: MediaSessionFactoryImpl.java From PainlessMusicPlayer with Apache License 2.0 | 5 votes |
@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 11
Source File: MediaSessionManager.java From Musicoco with Apache License 2.0 | 5 votes |
/** * 初始化并激活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 12
Source File: PlaybackServiceStatusHelper.java From odyssey with GNU General Public License v3.0 | 5 votes |
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 13
Source File: VideoFragment.java From leanback-homescreen-channels with Apache License 2.0 | 5 votes |
@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 14
Source File: PlaybackFragment.java From leanback-assistant with Apache License 2.0 | 5 votes |
@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 15
Source File: RadioPlayerService.java From monkeyboard-radio-android with GNU General Public License v3.0 | 4 votes |
@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 16
Source File: MusicService.java From MusicPlayer with GNU General Public License v3.0 | 4 votes |
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 |
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 LyricHere with Apache License 2.0 | 4 votes |
@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); }
Example 19
Source File: MusicService.java From YTPlayer with GNU General Public License v3.0 | 4 votes |
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); }
Example 20
Source File: PlayerService.java From AndroidAudioExample with MIT License | 4 votes |
@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(); }