Search in sources :

Example 6 with PlaybackState

use of android.media.session.PlaybackState in project Gadgetbridge by Freeyourgadget.

the class NotificationListener method handleMediaSessionNotification.

/**
     * Try to handle media session notifications that tell info about the current play state.
     *
     * @param notification The notification to handle.
     * @return true if notification was handled, false otherwise
     */
public boolean handleMediaSessionNotification(Notification notification) {
    // this code requires Android 5.0 or newer
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        return false;
    }
    MusicSpec musicSpec = new MusicSpec();
    MusicStateSpec stateSpec = new MusicStateSpec();
    Bundle extras = notification.extras;
    if (extras == null)
        return false;
    if (extras.get(Notification.EXTRA_MEDIA_SESSION) == null)
        return false;
    MediaController c;
    try {
        c = new MediaController(getApplicationContext(), (MediaSession.Token) extras.get(Notification.EXTRA_MEDIA_SESSION));
        PlaybackState s = c.getPlaybackState();
        stateSpec.position = (int) (s.getPosition() / 1000);
        stateSpec.playRate = Math.round(100 * s.getPlaybackSpeed());
        stateSpec.repeat = 1;
        stateSpec.shuffle = 1;
        switch(s.getState()) {
            case PlaybackState.STATE_PLAYING:
                stateSpec.state = MusicStateSpec.STATE_PLAYING;
                break;
            case PlaybackState.STATE_STOPPED:
                stateSpec.state = MusicStateSpec.STATE_STOPPED;
                break;
            case PlaybackState.STATE_PAUSED:
                stateSpec.state = MusicStateSpec.STATE_PAUSED;
                break;
            default:
                stateSpec.state = MusicStateSpec.STATE_UNKNOWN;
                break;
        }
        MediaMetadata d = c.getMetadata();
        if (d == null)
            return false;
        if (d.containsKey(MediaMetadata.METADATA_KEY_ARTIST))
            musicSpec.artist = d.getString(MediaMetadata.METADATA_KEY_ARTIST);
        if (d.containsKey(MediaMetadata.METADATA_KEY_ALBUM))
            musicSpec.album = d.getString(MediaMetadata.METADATA_KEY_ALBUM);
        if (d.containsKey(MediaMetadata.METADATA_KEY_TITLE))
            musicSpec.track = d.getString(MediaMetadata.METADATA_KEY_TITLE);
        if (d.containsKey(MediaMetadata.METADATA_KEY_DURATION))
            musicSpec.duration = (int) d.getLong(MediaMetadata.METADATA_KEY_DURATION) / 1000;
        if (d.containsKey(MediaMetadata.METADATA_KEY_NUM_TRACKS))
            musicSpec.trackCount = (int) d.getLong(MediaMetadata.METADATA_KEY_NUM_TRACKS);
        if (d.containsKey(MediaMetadata.METADATA_KEY_TRACK_NUMBER))
            musicSpec.trackNr = (int) d.getLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER);
        // finally, tell the device about it
        GBApplication.deviceService().onSetMusicInfo(musicSpec);
        GBApplication.deviceService().onSetMusicState(stateSpec);
        return true;
    } catch (NullPointerException e) {
        return false;
    }
}
Also used : MusicStateSpec(nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec) MusicSpec(nodomain.freeyourgadget.gadgetbridge.model.MusicSpec) MediaController(android.media.session.MediaController) Bundle(android.os.Bundle) MediaMetadata(android.media.MediaMetadata) PlaybackState(android.media.session.PlaybackState)

Example 7 with PlaybackState

use of android.media.session.PlaybackState in project android_frameworks_base by crdroidandroid.

the class MediaSessions method dump.

private static void dump(int n, PrintWriter writer, MediaController c) {
    writer.println("  Controller " + n + ": " + c.getPackageName());
    final Bundle extras = c.getExtras();
    final long flags = c.getFlags();
    final MediaMetadata mm = c.getMetadata();
    final PlaybackInfo pi = c.getPlaybackInfo();
    final PlaybackState playbackState = c.getPlaybackState();
    final List<QueueItem> queue = c.getQueue();
    final CharSequence queueTitle = c.getQueueTitle();
    final int ratingType = c.getRatingType();
    final PendingIntent sessionActivity = c.getSessionActivity();
    writer.println("    PlaybackState: " + Util.playbackStateToString(playbackState));
    writer.println("    PlaybackInfo: " + Util.playbackInfoToString(pi));
    if (mm != null) {
        writer.println("  MediaMetadata.desc=" + mm.getDescription());
    }
    writer.println("    RatingType: " + ratingType);
    writer.println("    Flags: " + flags);
    if (extras != null) {
        writer.println("    Extras:");
        for (String key : extras.keySet()) {
            writer.println("      " + key + "=" + extras.get(key));
        }
    }
    if (queueTitle != null) {
        writer.println("    QueueTitle: " + queueTitle);
    }
    if (queue != null && !queue.isEmpty()) {
        writer.println("    Queue:");
        for (QueueItem qi : queue) {
            writer.println("      " + qi);
        }
    }
    if (pi != null) {
        writer.println("    sessionActivity: " + sessionActivity);
    }
}
Also used : Bundle(android.os.Bundle) PlaybackInfo(android.media.session.MediaController.PlaybackInfo) MediaMetadata(android.media.MediaMetadata) PlaybackState(android.media.session.PlaybackState) QueueItem(android.media.session.MediaSession.QueueItem) PendingIntent(android.app.PendingIntent)

Example 8 with PlaybackState

use of android.media.session.PlaybackState in project android_frameworks_base by crdroidandroid.

the class QueueView method updateQueue.

/**
     * @param queue
     * @return whether the queue size has changed
     */
public boolean updateQueue(List<MediaSession.QueueItem> queue) {
    int queueSizeBefore = mAdapter.getCount();
    mQueue.clear();
    if (queue != null) {
        // add everything *after* the currently playing item
        boolean foundNowPlaying = false;
        final PlaybackState playbackState = mController.getPlaybackState();
        long activeQueueId = -1;
        if (playbackState != null) {
            activeQueueId = playbackState.getActiveQueueItemId();
        }
        for (int i = 0; i < queue.size() && mQueue.size() < getMaxQueueRowCount(); i++) {
            final MediaSession.QueueItem item = queue.get(i);
            if (!foundNowPlaying && activeQueueId != -1 && activeQueueId == item.getQueueId()) {
                foundNowPlaying = true;
                continue;
            }
            if (foundNowPlaying) {
                mQueue.add(item);
            }
        }
        // add everything
        if (!foundNowPlaying) {
            for (int i = 0; i < getMaxQueueRowCount() && i < queue.size(); i++) {
                mQueue.add(queue.get(i));
            }
        }
    }
    mAdapter.notifyDataSetChanged();
    return mAdapter.getCount() != queueSizeBefore;
}
Also used : MediaSession(android.media.session.MediaSession) PlaybackState(android.media.session.PlaybackState)

Example 9 with PlaybackState

use of android.media.session.PlaybackState in project android_frameworks_base by AOSPA.

the class MediaSessionRecord method getStateWithUpdatedPosition.

private PlaybackState getStateWithUpdatedPosition() {
    PlaybackState state;
    long duration = -1;
    synchronized (mLock) {
        state = mPlaybackState;
        if (mMetadata != null && mMetadata.containsKey(MediaMetadata.METADATA_KEY_DURATION)) {
            duration = mMetadata.getLong(MediaMetadata.METADATA_KEY_DURATION);
        }
    }
    PlaybackState result = null;
    if (state != null) {
        if (state.getState() == PlaybackState.STATE_PLAYING || state.getState() == PlaybackState.STATE_FAST_FORWARDING || state.getState() == PlaybackState.STATE_REWINDING) {
            long updateTime = state.getLastPositionUpdateTime();
            long currentTime = SystemClock.elapsedRealtime();
            if (updateTime > 0) {
                long position = (long) (state.getPlaybackSpeed() * (currentTime - updateTime)) + state.getPosition();
                if (duration >= 0 && position > duration) {
                    position = duration;
                } else if (position < 0) {
                    position = 0;
                }
                PlaybackState.Builder builder = new PlaybackState.Builder(state);
                builder.setState(state.getState(), position, state.getPlaybackSpeed(), currentTime);
                result = builder.build();
            }
        }
    }
    return result == null ? state : result;
}
Also used : PlaybackState(android.media.session.PlaybackState)

Example 10 with PlaybackState

use of android.media.session.PlaybackState in project android_frameworks_base by DirtyUnicorns.

the class MediaSessions method dump.

private static void dump(int n, PrintWriter writer, MediaController c) {
    writer.println("  Controller " + n + ": " + c.getPackageName());
    final Bundle extras = c.getExtras();
    final long flags = c.getFlags();
    final MediaMetadata mm = c.getMetadata();
    final PlaybackInfo pi = c.getPlaybackInfo();
    final PlaybackState playbackState = c.getPlaybackState();
    final List<QueueItem> queue = c.getQueue();
    final CharSequence queueTitle = c.getQueueTitle();
    final int ratingType = c.getRatingType();
    final PendingIntent sessionActivity = c.getSessionActivity();
    writer.println("    PlaybackState: " + Util.playbackStateToString(playbackState));
    writer.println("    PlaybackInfo: " + Util.playbackInfoToString(pi));
    if (mm != null) {
        writer.println("  MediaMetadata.desc=" + mm.getDescription());
    }
    writer.println("    RatingType: " + ratingType);
    writer.println("    Flags: " + flags);
    if (extras != null) {
        writer.println("    Extras:");
        for (String key : extras.keySet()) {
            writer.println("      " + key + "=" + extras.get(key));
        }
    }
    if (queueTitle != null) {
        writer.println("    QueueTitle: " + queueTitle);
    }
    if (queue != null && !queue.isEmpty()) {
        writer.println("    Queue:");
        for (QueueItem qi : queue) {
            writer.println("      " + qi);
        }
    }
    if (pi != null) {
        writer.println("    sessionActivity: " + sessionActivity);
    }
}
Also used : Bundle(android.os.Bundle) PlaybackInfo(android.media.session.MediaController.PlaybackInfo) MediaMetadata(android.media.MediaMetadata) PlaybackState(android.media.session.PlaybackState) QueueItem(android.media.session.MediaSession.QueueItem) PendingIntent(android.app.PendingIntent)

Aggregations

PlaybackState (android.media.session.PlaybackState)13 MediaMetadata (android.media.MediaMetadata)6 Bundle (android.os.Bundle)6 PendingIntent (android.app.PendingIntent)5 PlaybackInfo (android.media.session.MediaController.PlaybackInfo)5 QueueItem (android.media.session.MediaSession.QueueItem)5 MediaSession (android.media.session.MediaSession)2 MediaController (android.media.session.MediaController)1 MusicSpec (nodomain.freeyourgadget.gadgetbridge.model.MusicSpec)1 MusicStateSpec (nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec)1