当前位置: 首页>>代码示例>>Java>>正文


Java MediaSessionCompat.setCallback方法代码示例

本文整理汇总了Java中android.support.v4.media.session.MediaSessionCompat.setCallback方法的典型用法代码示例。如果您正苦于以下问题:Java MediaSessionCompat.setCallback方法的具体用法?Java MediaSessionCompat.setCallback怎么用?Java MediaSessionCompat.setCallback使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.support.v4.media.session.MediaSessionCompat的用法示例。


在下文中一共展示了MediaSessionCompat.setCallback方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: setUpRemoteControlClient

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
private void setUpRemoteControlClient() {
    final Intent mediaButtonIntent = new Intent(Intent.ACTION_MEDIA_BUTTON);
    mediaButtonIntent.setComponent(mMediaButtonReceiverComponent);

    mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    mMediaSession = new MediaSessionCompat(getApplication(), "TAG", mMediaButtonReceiverComponent, null);
    mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    PlaybackStateCompat playbackStateCompat = new PlaybackStateCompat.Builder()
            .setActions(
                    PlaybackStateCompat.ACTION_SEEK_TO |
                            PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS |
                            PlaybackStateCompat.ACTION_SKIP_TO_NEXT |
                            PlaybackStateCompat.ACTION_PLAY |
                            PlaybackStateCompat.ACTION_PAUSE |
                            PlaybackStateCompat.ACTION_STOP
            )
            .setState(isPlaying() ? PlaybackStateCompat.STATE_PLAYING : PlaybackStateCompat.STATE_PAUSED, position(), 1.0f)
            .build();

    mMediaSession.setPlaybackState(playbackStateCompat);
    mMediaSession.setCallback(mMediaSessionCallback);
    mMediaSession.setActive(true);
    updateRemoteControlClient(META_CHANGED);
    mTransportController = mMediaSession.getController().getTransportControls();

}
 
开发者ID:PhoenixDevTeam,项目名称:Phoenix-for-VK,代码行数:27,代码来源:MusicPlaybackService.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: createMediaSession

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
private MediaSessionCompat createMediaSession() {
    Context context = getContext();
    MediaSessionCompat mediaSession =
            new MediaSessionCompat(context, context.getString(R.string.app_name),
                    new ComponentName(context, getButtonReceiverClass()), 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;
}
 
开发者ID:mogoweb,项目名称:365browser,代码行数:25,代码来源:MediaNotificationManager.java

示例4: initializeMediaSession

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
private void initializeMediaSession() {
    mSession = new MediaSessionCompat(this, TAG);
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS
                    | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mSession.setActive(true);
    MediaControllerCompat.setMediaController(this, mSession.getController());

    MediaMetadataCompat metadata = new MediaMetadataCompat.Builder()
            .putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, mMovieView.getTitle())
            .build();
    mSession.setMetadata(metadata);

    MediaSessionCallback mMediaSessionCallback = new MediaSessionCallback(mMovieView);
    mSession.setCallback(mMediaSessionCallback);

    int state =
            mMovieView.isPlaying()
                    ? PlaybackStateCompat.STATE_PLAYING
                    : PlaybackStateCompat.STATE_PAUSED;
    updatePlaybackState(
            state,
            MEDIA_ACTIONS_ALL,
            mMovieView.getCurrentPosition(),
            mMovieView.getVideoResourceId());
}
 
开发者ID:googlesamples,项目名称:android-PictureInPicture,代码行数:27,代码来源:MediaSessionPlaybackActivity.java

示例5: onCreate

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
@Override
public void onCreate() {
    Log.d(TAG, "onCreate");
    super.onCreate();
    castSessionManager = CastContext.getSharedInstance(this).getSessionManager();

    // Create a MediaSessionCompat
    mediaSession = new MediaSessionCompat(this, TAG);

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

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

    // MySessionCallback() has methods that handle callbacks from a media controller
    mediaSession.setCallback(mediaSessionCallback);

    // Set the session's token so that client activities can communicate with it.
    setSessionToken(mediaSession.getSessionToken());
}
 
开发者ID:aschober,项目名称:vinyl-cast,代码行数:25,代码来源:MediaRecorderService.java

示例6: createMediaSession

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
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;
}
 
开发者ID:rkshuai,项目名称:chromium-for-android-56-debug-video,代码行数:27,代码来源:MediaNotificationManager.java

示例7: onCreate

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

    // Create a new MediaSession.
    mSession = new MediaSessionCompat(this, "MusicService");
    mCallback = new MediaSessionCallback();
    mSession.setCallback(mCallback);
    mSession.setFlags(
            MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSessionCompat.FLAG_HANDLES_QUEUE_COMMANDS |
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    setSessionToken(mSession.getSessionToken());

    mMediaNotificationManager = new MediaNotificationManager(this);

    mPlayback = new MediaPlayerAdapter(this, new MediaPlayerListener());
    Log.d(TAG, "onCreate: MusicService creating MediaSession, and MediaNotificationManager");
}
 
开发者ID:nazmulidris,项目名称:mediasession-mediaplayer,代码行数:20,代码来源:MusicService.java

示例8: 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

示例9: onCreate

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_video_play);

    mPlayPauseButton = (ImageButton) findViewById(R.id.videoPlayPauseButton);
    mSurfaceView = (SurfaceView) findViewById(R.id.videoSurfaceView);

    Intent callingIntent = this.getIntent();
    if(callingIntent != null) {
        mVideoUri = callingIntent.getData();
    }

    mSession = new MediaSessionCompat(this, TAG);
    mSession.setCallback(new MediaSessionCallback(this));
    mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mPBuilder = new PlaybackStateCompat.Builder();
    mController = new MediaControllerCompat(this, mSession);
    mControllerTransportControls = mController.getTransportControls();

}
 
开发者ID:mobapptuts,项目名称:media-thumbnail-viewer,代码行数:22,代码来源:VideoPlayActivity.java

示例10: doOpenSession

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
private void doOpenSession() {
    final ComponentName mediaButtonReceiver = new ComponentName(mContext,
            MediaButtonReceiver.class);

    final PendingIntent broadcastIntent = PendingIntent
            .getBroadcast(mContext, 1, new Intent(mContext, MediaButtonReceiver.class),
                    PendingIntent.FLAG_UPDATE_CURRENT);

    final MediaSessionCompat mediaSession = new MediaSessionCompat(mContext, TAG,
            mediaButtonReceiver, broadcastIntent);

    mediaSession.setCallback(new MediaSessionCallback(mContext));
    mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setActive(true);
    mediaSession.setSessionActivity(PendingIntent.getActivity(mContext, 1,
            new Intent(mContext, NowPlayingActivity.class), PendingIntent.FLAG_UPDATE_CURRENT));

    mMediaSession = mediaSession;

    Handlers.runOnIoThread(() -> reportMediaAndState(mediaSession));
}
 
开发者ID:Doctoror,项目名称:PainlessMusicPlayer,代码行数:23,代码来源:MediaSessionHolder.java

示例11: 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

示例12: MediaSessionWrapper

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
/**
 * 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);
}
 
开发者ID:tvbarthel,项目名称:Cheerleader,代码行数:24,代码来源:MediaSessionWrapper.java

示例13: onCreate

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
@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);
}
 
开发者ID:Fleker,项目名称:CumulusTV,代码行数:21,代码来源:CumulusBrowseService.java

示例14: createMediaSession

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
private MediaSessionCompat createMediaSession(Context context) {
    // create a media session
    MediaSessionCompat session = new MediaSessionCompat(context, LOG_TAG);
    session.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    session.setPlaybackState(getPlaybackState());
    session.setCallback(new MediaSessionCallback());
    if (mStation != null) {
        session.setMetadata(getMetadata(context, mStation, null));
    }
    setSessionToken(session.getSessionToken());

    return session;
}
 
开发者ID:sanjaysingh1990,项目名称:Exoplayer2Radio,代码行数:14,代码来源:PlayerService.java

示例15: onCreate

import android.support.v4.media.session.MediaSessionCompat; //导入方法依赖的package包/类
@Override
public void onCreate() {
  Timber.i("debug: Creating service");
  mHandler = new ServiceHandler(this);

  final Context appContext = getApplicationContext();
  mWifiLock = ((WifiManager) appContext.getSystemService(Context.WIFI_SERVICE))
      .createWifiLock(WifiManager.WIFI_MODE_FULL, "QuranAudioLock");
  mNotificationManager = (NotificationManager) appContext.getSystemService(NOTIFICATION_SERVICE);

  // create the Audio Focus Helper, if the Audio Focus feature is available
  mAudioFocusHelper = new AudioFocusHelper(appContext, this);

  mBroadcastManager = LocalBroadcastManager.getInstance(appContext);

  ComponentName receiver = new ComponentName(this, MediaButtonReceiver.class);
  mMediaSession = new MediaSessionCompat(appContext, "QuranMediaSession", receiver, null);
  mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
      MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
  mMediaSession.setCallback(new MediaSessionCallback());

  mNotificationColor = ContextCompat.getColor(this, R.color.audio_notification_color);
  try {
    // for Android Wear, use a 1x1 Bitmap with the notification color
    mDisplayIcon = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mDisplayIcon);
    canvas.drawColor(mNotificationColor);
  } catch (OutOfMemoryError oom) {
    Crashlytics.logException(oom);
  }
}
 
开发者ID:Elias33,项目名称:Quran,代码行数:32,代码来源:AudioService.java


注:本文中的android.support.v4.media.session.MediaSessionCompat.setCallback方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。