Java Code Examples for android.media.session.MediaSession#setActive()

The following examples show how to use android.media.session.MediaSession#setActive() . 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: NotificationService.java    From RoMote with Apache License 2.0 5 votes vote down vote up
private void setUpMediaSession() {
    mediaSession = new MediaSession(this, TAG);
    mediaSession.setActive(true);
    mediaSession.setFlags(MediaSession.FLAG_HANDLES_MEDIA_BUTTONS |
            MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
    mediaSession.setPlaybackState(
            new PlaybackState.Builder()
                    .setState(PlaybackState.STATE_PLAYING, 0L, 0F)
                    .setActions(PlaybackState.ACTION_PAUSE |
                            PlaybackState.ACTION_PLAY |
                            PlaybackState.ACTION_REWIND |
                            PlaybackState.ACTION_FAST_FORWARD)
                    .build());
    mediaSession.setMetadata(new MediaMetadata.Builder().build());
}
 
Example 2
Source File: AutoMediaBrowserService.java    From AndroidDemoProjects with Apache License 2.0 5 votes vote down vote up
private void initMediaSession() {
    mMediaSession = new MediaSession( this, "Android Auto Audio Demo" );
    mMediaSession.setActive( true );
    mMediaSession.setCallback( mMediaSessionCallback );

    mMediaSessionToken = mMediaSession.getSessionToken();
    setSessionToken( mMediaSessionToken );
}
 
Example 3
Source File: MusicPlayerService.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onCreate() {
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidSeek);
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mediaSession = new MediaSession(this, "telegramAudioPlayer");
        playbackState = new PlaybackState.Builder();
        albumArtPlaceholder = Bitmap.createBitmap(AndroidUtilities.dp(102), AndroidUtilities.dp(102), Bitmap.Config.ARGB_8888);
        Drawable placeholder = getResources().getDrawable(R.drawable.nocover_big);
        placeholder.setBounds(0, 0, albumArtPlaceholder.getWidth(), albumArtPlaceholder.getHeight());
        placeholder.draw(new Canvas(albumArtPlaceholder));
        mediaSession.setCallback(new MediaSession.Callback() {
            @Override
            public void onPlay() {
                MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
            }

            @Override
            public void onPause() {
                MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
            }

            @Override
            public void onSkipToNext() {
                MediaController.getInstance().playNextMessage();
            }

            @Override
            public void onSkipToPrevious() {
                MediaController.getInstance().playPreviousMessage();
            }

            @Override
            public void onStop() {
                //stopSelf();
            }
        });
        mediaSession.setActive(true);
    }

    super.onCreate();
}
 
Example 4
Source File: MusicPlayerService.java    From TelePlus-Android with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onCreate() {
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidSeek);
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mediaSession = new MediaSession(this, "telegramAudioPlayer");
        playbackState = new PlaybackState.Builder();
        albumArtPlaceholder = Bitmap.createBitmap(AndroidUtilities.dp(102), AndroidUtilities.dp(102), Bitmap.Config.ARGB_8888);
        Drawable placeholder = getResources().getDrawable(R.drawable.nocover_big);
        placeholder.setBounds(0, 0, albumArtPlaceholder.getWidth(), albumArtPlaceholder.getHeight());
        placeholder.draw(new Canvas(albumArtPlaceholder));
        mediaSession.setCallback(new MediaSession.Callback() {
            @Override
            public void onPlay() {
                MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
            }

            @Override
            public void onPause() {
                MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
            }

            @Override
            public void onSkipToNext() {
                MediaController.getInstance().playNextMessage();
            }

            @Override
            public void onSkipToPrevious() {
                MediaController.getInstance().playPreviousMessage();
            }

            @Override
            public void onStop() {
                //stopSelf();
            }
        });
        mediaSession.setActive(true);
    }

    super.onCreate();
}
 
Example 5
Source File: MusicPlayerService.java    From Telegram-FOSS with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onCreate() {
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidSeek);
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.httpFileDidLoad);
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.fileDidLoad);
    }
    imageReceiver = new ImageReceiver(null);
    imageReceiver.setDelegate((imageReceiver, set, thumb, memCache) -> {
        if (set && !TextUtils.isEmpty(loadingFilePath)) {
            MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
            if (messageObject != null) {
                createNotification(messageObject, true);
            }
            loadingFilePath = null;
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mediaSession = new MediaSession(this, "telegramAudioPlayer");
        playbackState = new PlaybackState.Builder();
        albumArtPlaceholder = Bitmap.createBitmap(AndroidUtilities.dp(102), AndroidUtilities.dp(102), Bitmap.Config.ARGB_8888);
        Drawable placeholder = getResources().getDrawable(R.drawable.nocover_big);
        placeholder.setBounds(0, 0, albumArtPlaceholder.getWidth(), albumArtPlaceholder.getHeight());
        placeholder.draw(new Canvas(albumArtPlaceholder));
        mediaSession.setCallback(new MediaSession.Callback() {
            @Override
            public void onPlay() {
                MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
            }

            @Override
            public void onPause() {
                MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
            }

            @Override
            public void onSkipToNext() {
                MediaController.getInstance().playNextMessage();
            }

            @Override
            public void onSkipToPrevious() {
                MediaController.getInstance().playPreviousMessage();
            }

            @Override
            public void onSeekTo(long pos) {
                MessageObject object = MediaController.getInstance().getPlayingMessageObject();
                if (object != null) {
                    MediaController.getInstance().seekToProgress(object, pos / 1000 / (float) object.getDuration());
                    updatePlaybackState(pos);
                }
            }

            @Override
            public void onStop() {
                //stopSelf();
            }
        });
        mediaSession.setActive(true);
    }

    registerReceiver(headsetPlugReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));

    super.onCreate();
}
 
Example 6
Source File: MusicPlayerService.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
@Override
public void onCreate() {
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    for (int a = 0; a < UserConfig.MAX_ACCOUNT_COUNT; a++) {
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingDidSeek);
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.messagePlayingPlayStateChanged);
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.httpFileDidLoad);
        NotificationCenter.getInstance(a).addObserver(this, NotificationCenter.fileDidLoad);
    }
    imageReceiver = new ImageReceiver(null);
    imageReceiver.setDelegate((imageReceiver, set, thumb, memCache) -> {
        if (set && !TextUtils.isEmpty(loadingFilePath)) {
            MessageObject messageObject = MediaController.getInstance().getPlayingMessageObject();
            if (messageObject != null) {
                createNotification(messageObject, true);
            }
            loadingFilePath = null;
        }
    });

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        mediaSession = new MediaSession(this, "telegramAudioPlayer");
        playbackState = new PlaybackState.Builder();
        albumArtPlaceholder = Bitmap.createBitmap(AndroidUtilities.dp(102), AndroidUtilities.dp(102), Bitmap.Config.ARGB_8888);
        Drawable placeholder = getResources().getDrawable(R.drawable.nocover_big);
        placeholder.setBounds(0, 0, albumArtPlaceholder.getWidth(), albumArtPlaceholder.getHeight());
        placeholder.draw(new Canvas(albumArtPlaceholder));
        mediaSession.setCallback(new MediaSession.Callback() {
            @Override
            public void onPlay() {
                MediaController.getInstance().playMessage(MediaController.getInstance().getPlayingMessageObject());
            }

            @Override
            public void onPause() {
                MediaController.getInstance().pauseMessage(MediaController.getInstance().getPlayingMessageObject());
            }

            @Override
            public void onSkipToNext() {
                MediaController.getInstance().playNextMessage();
            }

            @Override
            public void onSkipToPrevious() {
                MediaController.getInstance().playPreviousMessage();
            }

            @Override
            public void onSeekTo(long pos) {
                MessageObject object = MediaController.getInstance().getPlayingMessageObject();
                if (object != null) {
                    MediaController.getInstance().seekToProgress(object, pos / 1000 / (float) object.getDuration());
                    updatePlaybackState(pos);
                }
            }

            @Override
            public void onStop() {
                //stopSelf();
            }
        });
        mediaSession.setActive(true);
    }

    registerReceiver(headsetPlugReceiver, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));

    super.onCreate();
}