當前位置: 首頁>>代碼示例>>Java>>正文


Java MediaSessionCompat.setMediaButtonReceiver方法代碼示例

本文整理匯總了Java中android.support.v4.media.session.MediaSessionCompat.setMediaButtonReceiver方法的典型用法代碼示例。如果您正苦於以下問題:Java MediaSessionCompat.setMediaButtonReceiver方法的具體用法?Java MediaSessionCompat.setMediaButtonReceiver怎麽用?Java MediaSessionCompat.setMediaButtonReceiver使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在android.support.v4.media.session.MediaSessionCompat的用法示例。


在下文中一共展示了MediaSessionCompat.setMediaButtonReceiver方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: initializeMediaSession

import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
private void initializeMediaSession() {
    mMediaSession = new MediaSessionCompat(getContext(), TAG);
    mMediaSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                    MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    mMediaSession.setMediaButtonReceiver(null);
    mStateBuilder = new PlaybackStateCompat.Builder()
            .setActions(
                    PlaybackStateCompat.ACTION_PLAY |
                            PlaybackStateCompat.ACTION_PAUSE |
                            PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                            PlaybackStateCompat.ACTION_PLAY_PAUSE);

    mMediaSession.setPlaybackState(mStateBuilder.build());
    mMediaSession.setCallback(new MySessionCallback());
    mMediaSession.setActive(true);
}
 
開發者ID:twisstosin,項目名稱:UdacityBakingAndroid,代碼行數:19,代碼來源:StepFragment.java

示例2: onCreate

import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
@Override
public void onCreate() {
    super.onCreate();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        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(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)), null);
    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();
}
 
開發者ID:SergeyVinyar,項目名稱:AndroidAudioExample,代碼行數:43,代碼來源:PlayerService.java

示例3: setupMediaSession

import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
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, "RetroMusicPlayer", 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);
}
 
開發者ID:h4h13,項目名稱:RetroMusicPlayer,代碼行數:53,代碼來源:MusicService.java

示例4: initMediaSession

import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
void initMediaSession() {
    // NOTE: all this is so that when you press pause/play in the app, we can capture the
    // media control event, so that other apps DON'T (ie, google play music, plex, etc).
    // ideally we could do something useful with this, but for not, just eat it.

    try {
        ComponentName mediaButtonReceiver = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
        mediaSessionCompat = new MediaSessionCompat(getApplicationContext(), "SAGETVMINICLIENT", mediaButtonReceiver, null);
        mediaSessionCompat.setCallback(new MediaSessionCompat.Callback() {
            @Override
            public void onCommand(String command, Bundle extras, ResultReceiver cb) {
                log.debug("Audio Session Callback Handler: Command {}", command);
            }
        });
        mediaSessionCompat.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS);

        Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
        mediaButtonIntent.setClass(this, MediaButtonReceiver.class);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, mediaButtonIntent, 0);
        mediaSessionCompat.setMediaButtonReceiver(pendingIntent);
        mediaSessionCompat.setActive(true);
        log.debug("Media Session is setup to capture pause/play. session: "+mediaSessionCompat.getSessionToken());
    } catch (Throwable t) {
        log.error("Failed to capture the media session", t);
    }
}
 
開發者ID:OpenSageTV,項目名稱:sagetv-miniclient,代碼行數:27,代碼來源:MiniClientGDXActivity.java

示例5: setupMediaSession

import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
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);
}
 
開發者ID:aliumujib,項目名稱:Orin,代碼行數:52,代碼來源:MusicService.java

示例6: initializeMediaSession

import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
private void initializeMediaSession() {
    // Create a MediaSessionCompat.
    mMediaSession = new MediaSessionCompat(getContext(), StepDetailFragment.class.getSimpleName());

    // Enable callbacks from MediaButtons and TransportControls.
    mMediaSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
                    MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    // Do not let MediaButtons restart the player when the app is not visible.
    mMediaSession.setMediaButtonReceiver(null);

    // Set an initial PlaybackState with ACTION_PLAY, so media buttons can start the player.
    mStateBuilder = new PlaybackStateCompat.Builder()
            .setActions(
                    PlaybackStateCompat.ACTION_PLAY |
                            PlaybackStateCompat.ACTION_PAUSE |
                            PlaybackStateCompat.ACTION_PLAY_PAUSE);

    mMediaSession.setPlaybackState(mStateBuilder.build());


    // MySessionCallback has methods that handle callbacks from a media controller.
    mMediaSession.setCallback(new MediaSessionCallback());

    // Start the Media Session since the activity is active.
    mMediaSession.setActive(true);

}
 
開發者ID:harrynp,項目名稱:BakingApp,代碼行數:30,代碼來源:StepDetailFragment.java

示例7: mediaLockscreen

import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
@Override
public void mediaLockscreen() {
    mediaSessionLockscreen = new MediaSessionCompat(this, TAG);
    mediaSessionLockscreen.setCallback(new MediaSessionCompat.Callback() {
        @Override
        public void onPlay() {
            play();
        }

        @Override
        public void onPause() {
            pause();
        }

        @Override
        public void onSkipToNext() {
            playnext(true);
        }

        @Override
        public void onSkipToPrevious() {
            playprev(true);
        }

        @Override
        public void onStop() {
            stopSelf();
        }

        @Override
        public void onSeekTo(long pos) {
            seekto((int) pos);
        }

        @Override
        public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
            if (mediaButtonReceiver != null) {
                mediaButtonReceiver.onReceive(MusicXService.this, mediaButtonEvent);
            }
            return true;
        }
    });
    mediaSessionLockscreen.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    ComponentName buttonCom = new ComponentName(getApplicationContext(), MediaButtonReceiver.class);
    Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    intent.setComponent(buttonCom);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mediaSessionLockscreen.setMediaButtonReceiver(pendingIntent);
    mediaSessionLockscreen.setActive(true);
}
 
開發者ID:RajneeshSingh007,項目名稱:MusicX-music-player,代碼行數:51,代碼來源:MusicXService.java

示例8: PlaybackServiceStatusHelper

import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
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));

    Intent settingChangedIntent = new Intent(MESSAGE_HIDE_ARTWORK_CHANGED);
    settingChangedIntent.putExtra(MESSAGE_EXTRA_HIDE_ARTWORK_CHANGED_VALUE, mHideArtwork);
    mPlaybackService.sendBroadcast(settingChangedIntent);
}
 
開發者ID:gateship-one,項目名稱:odyssey,代碼行數:28,代碼來源:PlaybackServiceStatusHelper.java

示例9: initMediaSession

import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
/**
 * 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());
    MediaSessionCompat session = new MediaSessionCompat(mContext, TAG, mbrComponent, null);

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

    session.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
            | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

    PlaybackStateCompat.Builder state = new PlaybackStateCompat.Builder()
            .setActions(PlaybackStateCompat.ACTION_PLAY
                    | PlaybackStateCompat.ACTION_PLAY_PAUSE
                    | PlaybackStateCompat.ACTION_SEEK_TO
                    | PlaybackStateCompat.ACTION_PAUSE
                    | PlaybackStateCompat.ACTION_SKIP_TO_NEXT
                    | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS
                    | PlaybackStateCompat.ACTION_STOP)
            .setState(PlaybackStateCompat.STATE_NONE, 0, 0f);

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

    session.setPlaybackState(state.build());
    session.setActive(true);

    mMediaSession = session;
}
 
開發者ID:marverenic,項目名稱:Jockey,代碼行數:40,代碼來源:MusicPlayer.java

示例10: initMediaSession

import android.support.v4.media.session.MediaSessionCompat; //導入方法依賴的package包/類
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());

    }
 
開發者ID:SpongeBobSun,項目名稱:Prodigal,代碼行數:14,代碼來源:PlayerService.java


注:本文中的android.support.v4.media.session.MediaSessionCompat.setMediaButtonReceiver方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。