Search in sources :

Example 21 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class FullScreenPlayerActivity method connectToSession.

private void connectToSession(MediaSessionCompat.Token token) throws RemoteException {
    MediaControllerCompat mediaController = new MediaControllerCompat(FullScreenPlayerActivity.this, token);
    if (mediaController.getMetadata() == null) {
        finish();
        return;
    }
    setSupportMediaController(mediaController);
    mediaController.registerCallback(mCallback);
    PlaybackStateCompat state = mediaController.getPlaybackState();
    updatePlaybackState(state);
    MediaMetadataCompat metadata = mediaController.getMetadata();
    if (metadata != null) {
        updateMediaDescription(metadata.getDescription());
        updateDuration(metadata);
    }
    updateProgress();
    if (state != null && (state.getState() == PlaybackStateCompat.STATE_PLAYING || state.getState() == PlaybackStateCompat.STATE_BUFFERING)) {
        scheduleSeekbarUpdate();
    }
}
Also used : PlaybackStateCompat(android.support.v4.media.session.PlaybackStateCompat) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) MediaControllerCompat(android.support.v4.media.session.MediaControllerCompat)

Example 22 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class MusicService method onCreate.

/*
     * (non-Javadoc)
     * @see android.app.Service#onCreate()
     */
@Override
public void onCreate() {
    super.onCreate();
    LogHelper.d(TAG, "onCreate");
    mMusicProvider = new MusicProvider();
    // To make the app more responsive, fetch and cache catalog information now.
    // This can help improve the response time in the method
    // {@link #onLoadChildren(String, Result<List<MediaItem>>) onLoadChildren()}.
    mMusicProvider.retrieveMediaAsync(null);
    mPackageValidator = new PackageValidator(this);
    QueueManager queueManager = new QueueManager(mMusicProvider, getResources(), new QueueManager.MetadataUpdateListener() {

        @Override
        public void onMetadataChanged(MediaMetadataCompat metadata) {
            mSession.setMetadata(metadata);
        }

        @Override
        public void onMetadataRetrieveError() {
            mPlaybackManager.updatePlaybackState(getString(R.string.error_no_metadata));
        }

        @Override
        public void onCurrentQueueIndexUpdated(int queueIndex) {
            mPlaybackManager.handlePlayRequest();
        }

        @Override
        public void onQueueUpdated(String title, List<MediaSessionCompat.QueueItem> newQueue) {
            mSession.setQueue(newQueue);
            mSession.setQueueTitle(title);
        }
    });
    LocalPlayback playback = new LocalPlayback(this, mMusicProvider);
    mPlaybackManager = new PlaybackManager(this, getResources(), mMusicProvider, queueManager, playback);
    // Start a new MediaSession
    mSession = new MediaSessionCompat(this, "MusicService");
    setSessionToken(mSession.getSessionToken());
    mSession.setCallback(mPlaybackManager.getMediaSessionCallback());
    mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS | MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
    Context context = getApplicationContext();
    Intent intent = new Intent(context, NowPlayingActivity.class);
    PendingIntent pi = PendingIntent.getActivity(context, 99, /*request code*/
    intent, PendingIntent.FLAG_UPDATE_CURRENT);
    mSession.setSessionActivity(pi);
    mSessionExtras = new Bundle();
    CarHelper.setSlotReservationFlags(mSessionExtras, true, true, true);
    WearHelper.setSlotReservationFlags(mSessionExtras, true, true);
    WearHelper.setUseBackgroundFromTheme(mSessionExtras, true);
    mSession.setExtras(mSessionExtras);
    mPlaybackManager.updatePlaybackState(null);
    try {
        mMediaNotificationManager = new MediaNotificationManager(this);
    } catch (RemoteException e) {
        throw new IllegalStateException("Could not create a MediaNotificationManager", e);
    }
    if (!TvHelper.isTvUiMode(this)) {
        mCastSessionManager = CastContext.getSharedInstance(this).getSessionManager();
        mCastSessionManagerListener = new CastSessionManagerListener();
        mCastSessionManager.addSessionManagerListener(mCastSessionManagerListener, CastSession.class);
    }
    mMediaRouter = MediaRouter.getInstance(getApplicationContext());
    registerCarConnectionReceiver();
}
Also used : Context(android.content.Context) CastContext(com.google.android.gms.cast.framework.CastContext) MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Bundle(android.os.Bundle) PlaybackManager(com.example.android.uamp.playback.PlaybackManager) LocalPlayback(com.example.android.uamp.playback.LocalPlayback) Intent(android.content.Intent) PendingIntent(android.app.PendingIntent) MediaSessionCompat(android.support.v4.media.session.MediaSessionCompat) QueueManager(com.example.android.uamp.playback.QueueManager) PendingIntent(android.app.PendingIntent) MusicProvider(com.example.android.uamp.model.MusicProvider) RemoteException(android.os.RemoteException)

Example 23 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class LocalPlayback method play.

@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;
    tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = item.getDescription().getMediaId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mCurrentPosition = 0;
        mCurrentMediaId = mediaId;
    }
    if (mState == PlaybackStateCompat.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) {
        configMediaPlayerState();
    } else {
        mState = PlaybackStateCompat.STATE_STOPPED;
        // release everything except MediaPlayer
        relaxResources(false);
        MediaMetadataCompat track = mMusicProvider.getMusic(MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));
        //noinspection ResourceType
        String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
        if (source != null) {
            // Escape spaces for URLs
            source = source.replaceAll(" ", "%20");
        }
        try {
            createMediaPlayerIfNeeded();
            mState = PlaybackStateCompat.STATE_BUFFERING;
            mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mMediaPlayer.setDataSource(source);
            // Starts preparing the media player in the background. When
            // it's done, it will call our OnPreparedListener (that is,
            // the onPrepared() method on this class, since we set the
            // listener to 'this'). Until the media player is prepared,
            // we *cannot* call start() on it!
            mMediaPlayer.prepareAsync();
            // If we are streaming from the internet, we want to hold a
            // Wifi lock, which prevents the Wifi radio from going to
            // sleep while the song is playing.
            mWifiLock.acquire();
            if (mCallback != null) {
                mCallback.onPlaybackStatusChanged(mState);
            }
        } catch (IOException ex) {
            LogHelper.e(TAG, ex, "Exception playing song");
            if (mCallback != null) {
                mCallback.onError(ex.getMessage());
            }
        }
    }
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) IOException(java.io.IOException)

Example 24 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class MusicProviderTest method testFavorite.

@Test
public void testFavorite() throws Exception {
    MediaMetadataCompat metadata = provider.getShuffledMusic().iterator().next();
    String musicId = metadata.getDescription().getMediaId();
    assertFalse(provider.isFavorite(musicId));
    provider.setFavorite(musicId, true);
    assertTrue(provider.isFavorite(musicId));
    provider.setFavorite(musicId, false);
    assertFalse(provider.isFavorite(musicId));
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Test(org.junit.Test)

Example 25 with MediaMetadataCompat

use of android.support.v4.media.MediaMetadataCompat in project android-UniversalMusicPlayer by googlesamples.

the class MusicProviderTest method testSearchBySongTitle.

@Test
public void testSearchBySongTitle() throws Exception {
    int count = 0;
    for (MediaMetadataCompat metadata : provider.searchMusicBySongTitle("Romantic")) {
        String title = metadata.getString(MediaMetadataCompat.METADATA_KEY_TITLE);
        assertTrue(title.contains("Romantic"));
        count++;
    }
    assertEquals(2, count);
}
Also used : MediaMetadataCompat(android.support.v4.media.MediaMetadataCompat) Test(org.junit.Test)

Aggregations

MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)29 Test (org.junit.Test)13 MediaSessionCompat (android.support.v4.media.session.MediaSessionCompat)8 Bitmap (android.graphics.Bitmap)5 PlaybackStateCompat (android.support.v4.media.session.PlaybackStateCompat)4 Uri (android.net.Uri)3 Bundle (android.os.Bundle)3 MediaControllerCompat (android.support.v4.media.session.MediaControllerCompat)3 ArrayList (java.util.ArrayList)3 PendingIntent (android.app.PendingIntent)2 Context (android.content.Context)2 Intent (android.content.Intent)2 RemoteException (android.os.RemoteException)2 View (android.view.View)2 ImageView (android.widget.ImageView)2 TextView (android.widget.TextView)2 MediaInfo (com.google.android.gms.cast.MediaInfo)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 JSONObject (org.json.JSONObject)2 Configuration (android.content.res.Configuration)1