• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

Java MediaIDHelper类的典型用法和代码示例

java 2次浏览

本文整理汇总了Java中com.example.android.uamp.utils.MediaIDHelper的典型用法代码示例。如果您正苦于以下问题:Java MediaIDHelper类的具体用法?Java MediaIDHelper怎么用?Java MediaIDHelper使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。

MediaIDHelper类属于com.example.android.uamp.utils包,在下文中一共展示了MediaIDHelper类的23个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Java代码示例。

示例1: getView

点赞 3

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowser.MediaItem item = getItem(position);
    int itemState = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        itemState = MediaItemViewHolder.STATE_PLAYABLE;
        MediaController controller = ((Activity) getContext()).getMediaController();
        if (controller != null && controller.getMetadata() != null) {
            String currentPlaying = controller.getMetadata().getDescription().getMediaId();
            String musicId = MediaIDHelper.extractMusicIDFromMediaID(
                    item.getDescription().getMediaId());
            if (currentPlaying != null && currentPlaying.equals(musicId)) {
                PlaybackState pbState = controller.getPlaybackState();
                if (pbState == null || pbState.getState() == PlaybackState.STATE_ERROR) {
                    itemState = MediaItemViewHolder.STATE_NONE;
                } else if (pbState.getState() == PlaybackState.STATE_PLAYING) {
                    itemState = MediaItemViewHolder.STATE_PLAYING;
                } else {
                    itemState = MediaItemViewHolder.STATE_PAUSED;
                }
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
        item.getDescription(), itemState);
}
 

开发者ID:mrinalgit-dev,
项目名称:MrinalMusicPlayer,
代码行数:27,
代码来源:MediaBrowserFragment.java

示例2: getView

点赞 3

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowserCompat.MediaItem item = getItem(position);
    int state = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        state = MediaItemViewHolder.STATE_PLAYABLE;
        MediaControllerCompat controller = mMediaControllerProvider.getSupportMediaController();
        if (controller != null && controller.getMetadata() != null) {
            String currentPlaying = controller.getMetadata().getDescription().getMediaId();
            String musicId = MediaIDHelper.extractMusicIDFromMediaID(
                    item.getDescription().getMediaId());
            if (currentPlaying != null && currentPlaying.equals(musicId)) {
                if (controller.getPlaybackState().getState() == PlaybackStateCompat.STATE_PLAYING) {
                    state = MediaItemViewHolder.STATE_PLAYING;
                } else if (controller.getPlaybackState().getState() != PlaybackStateCompat.STATE_ERROR) {
                    state = MediaItemViewHolder.STATE_PAUSED;
                }
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent, item.getDescription(), state);
}
 

开发者ID:SoumyaParida,
项目名称:MyGaana-Universal,
代码行数:23,
代码来源:MediaBrowserFragment.java

示例3: onItemClicked

点赞 3

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Override
public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
                          RowPresenter.ViewHolder rowViewHolder, Row row) {

    MediaControllerCompat controller = MediaControllerCompat.getMediaController(getActivity());
    if (controller == null) {
        return;
    }
    MediaControllerCompat.TransportControls controls = controller.getTransportControls();
    MediaBrowserCompat.MediaItem mediaItem = (MediaBrowserCompat.MediaItem) item;

    if (!MediaIDHelper.isMediaItemPlaying(getActivity(), mediaItem)) {
        controls.playFromMediaId(mediaItem.getMediaId(), null);
    }

    Intent intent = new Intent(getActivity(), TvPlaybackActivity.class);
    Bundle bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
            getActivity(),
            ((ImageCardView) itemViewHolder.view).getMainImageView(),
            TvVerticalGridActivity.SHARED_ELEMENT_NAME).toBundle();

    getActivity().startActivity(intent, bundle);
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:24,
代码来源:TvVerticalGridFragment.java

示例4: setQueueFromMusic

点赞 3

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
public void setQueueFromMusic(String mediaId) {
    LogHelper.d(TAG, "setQueueFromMusic", mediaId);

    // The mediaId used here is not the unique musicId. This one comes from the
    // MediaBrowser, and is actually a "hierarchy-aware mediaID": a concatenation of
    // the hierarchy in MediaBrowser and the actual unique musicID. This is necessary
    // so we can build the correct playing queue, based on where the track was
    // selected from.
    boolean canReuseQueue = false;
    if (isSameBrowsingCategory(mediaId)) {
        canReuseQueue = setCurrentQueueItem(mediaId);
    }
    if (!canReuseQueue) {
        String queueTitle = mResources.getString(R.string.browse_musics_by_genre_subtitle,
                MediaIDHelper.extractBrowseCategoryValueFromMediaID(mediaId));
        setCurrentQueue(queueTitle,
                QueueHelper.getPlayingQueue(mediaId, mMusicProvider), mediaId);
    }
    updateMetadata();
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:21,
代码来源:QueueManager.java

示例5: setCustomAction

点赞 3

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
private void setCustomAction(PlaybackStateCompat.Builder stateBuilder) {
    MediaSessionCompat.QueueItem currentMusic = mQueueManager.getCurrentMusic();
    if (currentMusic == null) {
        return;
    }
    // Set appropriate "Favorite" icon on Custom action:
    String mediaId = currentMusic.getDescription().getMediaId();
    if (mediaId == null) {
        return;
    }
    String musicId = MediaIDHelper.extractMusicIDFromMediaID(mediaId);
    int favoriteIcon = mMusicProvider.isFavorite(musicId) ?
            R.drawable.ic_star_on : R.drawable.ic_star_off;
    LogHelper.d(TAG, "updatePlaybackState, setting Favorite custom action of music ",
            musicId, " current favorite=", mMusicProvider.isFavorite(musicId));
    Bundle customActionExtras = new Bundle();
    WearHelper.setShowCustomActionOnWear(customActionExtras, true);
    stateBuilder.addCustomAction(new PlaybackStateCompat.CustomAction.Builder(
            CUSTOM_ACTION_THUMBS_UP, mResources.getString(R.string.favorite), favoriteIcon)
            .setExtras(customActionExtras)
            .build());
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:23,
代码来源:PlaybackManager.java

示例6: onCustomAction

点赞 3

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Override
public void onCustomAction(@NonNull String action, Bundle extras) {
    if (CUSTOM_ACTION_THUMBS_UP.equals(action)) {
        LogHelper.i(TAG, "onCustomAction: favorite for current track");
        MediaSessionCompat.QueueItem currentMusic = mQueueManager.getCurrentMusic();
        if (currentMusic != null) {
            String mediaId = currentMusic.getDescription().getMediaId();
            if (mediaId != null) {
                String musicId = MediaIDHelper.extractMusicIDFromMediaID(mediaId);
                mMusicProvider.setFavorite(musicId, !mMusicProvider.isFavorite(musicId));
            }
        }
        // playback state needs to be updated because the "Favorite" icon on the
        // custom action will change to reflect the new favorite state.
        updatePlaybackState(null);
    } else {
        LogHelper.e(TAG, "Unsupported action: ", action);
    }
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:20,
代码来源:PlaybackManager.java

示例7: testIsSameBrowsingCategory

点赞 3

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Test
 public void testIsSameBrowsingCategory() throws Exception {
     QueueManager queueManager = createQueueManagerWithValidation(null, -1, null);

     Iterator<String> genres = provider.getGenres().iterator();
     String genre1 = genres.next();
     String genre2 = genres.next();
     List<MediaSessionCompat.QueueItem> queueGenre1 = QueueHelper.getPlayingQueue(
             MediaIDHelper.createMediaID(null, MediaIDHelper.MEDIA_ID_MUSICS_BY_GENRE, genre1),
             provider);
     List<MediaSessionCompat.QueueItem> queueGenre2 = QueueHelper.getPlayingQueue(
             MediaIDHelper.createMediaID(null, MediaIDHelper.MEDIA_ID_MUSICS_BY_GENRE, genre2),
             provider);

     // set the current queue
     queueManager.setCurrentQueue("Queue genre 1", queueGenre1);

     // the current music cannot be of same browsing category as one with genre 2
     assertFalse(queueManager.isSameBrowsingCategory(queueGenre2.get(0).getDescription().getMediaId()));

     // the current music needs to be of same browsing category as one with genre 1
     assertTrue(queueManager.isSameBrowsingCategory(queueGenre1.get(0).getDescription().getMediaId()));
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:24,
代码来源:QueueManagerTest.java

示例8: getView

点赞 3

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    MediaBrowser.MediaItem item = getItem(position);
    int state = MediaItemViewHolder.STATE_NONE;
    if (item.isPlayable()) {
        state = MediaItemViewHolder.STATE_PLAYABLE;
        MediaController controller = ((Activity) getContext()).getMediaController();
        if (controller != null && controller.getMetadata() != null) {
            String currentPlaying = controller.getMetadata().getDescription().getMediaId();
            String musicId = MediaIDHelper.extractMusicIDFromMediaID(
                    item.getDescription().getMediaId());
            if (currentPlaying != null && currentPlaying.equals(musicId)) {
                if (controller.getPlaybackState().getState() ==
                        PlaybackState.STATE_PLAYING) {
                    state = MediaItemViewHolder.STATE_PLAYING;
                } else if (controller.getPlaybackState().getState() !=
                        PlaybackState.STATE_ERROR) {
                    state = MediaItemViewHolder.STATE_PAUSED;
                }
            }
        }
    }
    return MediaItemViewHolder.setupView((Activity) getContext(), convertView, parent,
        item.getDescription(), state);
}
 

开发者ID:ErNaveen,
项目名称:UniversalMusicPlayer,
代码行数:26,
代码来源:MediaBrowserFragment.java

示例9: onPlayFromMediaId

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
    LogHelper.d(TAG, "playFromMediaId mediaId:", mediaId, "  extras=", extras);

    // The mediaId used here is not the unique musicId. This one comes from the
    // MediaBrowser, and is actually a "hierarchy-aware mediaID": a concatenation of
    // the hierarchy in MediaBrowser and the actual unique musicID. This is necessary
    // so we can build the correct playing queue, based on where the track was
    // selected from.
    mPlayingQueue = QueueHelper.getPlayingQueue(mediaId, mMusicProvider);
    mSession.setQueue(mPlayingQueue);
    String queueTitle = getString(R.string.browse_musics_by_genre_subtitle,
            MediaIDHelper.extractBrowseCategoryValueFromMediaID(mediaId));
    mSession.setQueueTitle(queueTitle);

    if (mPlayingQueue != null && !mPlayingQueue.isEmpty()) {
        // set the current index on queue from the media Id:
        mCurrentIndexOnQueue = QueueHelper.getMusicIndexOnQueue(mPlayingQueue, mediaId);

        if (mCurrentIndexOnQueue < 0) {
            LogHelper.e(TAG, "playFromMediaId: media ID ", mediaId,
                    " could not be found on queue. Ignoring.");
        } else {
            // play the music
            handlePlayRequest();
        }
    }
}
 

开发者ID:mrinalgit-dev,
项目名称:MrinalMusicPlayer,
代码行数:29,
代码来源:MusicService.java

示例10: getCurrentPlayingMusic

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
private MediaMetadata getCurrentPlayingMusic() {
    if (QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        MediaSession.QueueItem item = mPlayingQueue.get(mCurrentIndexOnQueue);
        if (item != null) {
            LogHelper.d(TAG, "getCurrentPlayingMusic for musicId=",
                    item.getDescription().getMediaId());
            return mMusicProvider.getMusic(
                    MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));
        }
    }
    return null;
}
 

开发者ID:mrinalgit-dev,
项目名称:MrinalMusicPlayer,
代码行数:13,
代码来源:MusicService.java

示例11: getCurrentPlayingMusic

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
private MediaMetadataCompat getCurrentPlayingMusic() {
    if (QueueHelper.isIndexPlayable(mCurrentIndexOnQueue, mPlayingQueue)) {
        MediaSessionCompat.QueueItem item = mPlayingQueue.get(mCurrentIndexOnQueue);
        if (item != null) {
            LogHelper.d(TAG, "getCurrentPlayingMusic for musicId=",
                    item.getDescription().getMediaId());
            return mMusicProvider.getMusic(
                    MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));
        }
    }
    return null;
}
 

开发者ID:SoumyaParida,
项目名称:MyGaana-Universal,
代码行数:13,
代码来源:MusicService.java

示例12: loadMedia

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
private void loadMedia(String mediaId, boolean autoPlay) throws
    TransientNetworkDisconnectionException, NoConnectionException, JSONException {
    String musicId = MediaIDHelper.extractMusicIDFromMediaID(mediaId);
    MediaMetadataCompat track = mMusicProvider.getMusic(musicId);
    if (!TextUtils.equals(mediaId, mCurrentMediaId)) {
        mCurrentMediaId = mediaId;
        mCurrentPosition = 0;
    }
    JSONObject customData = new JSONObject();
    customData.put(ITEM_ID, mediaId);
    MediaInfo media = toCastMediaMetadata(track, customData);
    mCastManager.loadMedia(media, autoPlay, mCurrentPosition, customData);
}
 

开发者ID:SoumyaParida,
项目名称:MyGaana-Universal,
代码行数:14,
代码来源:CastPlayback.java

示例13: createMediaItem

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
private MediaBrowserCompat.MediaItem createMediaItem(MediaMetadataCompat metadata) {
    // Since mediaMetadata fields are immutable, we need to create a copy, so we
    // can set a hierarchy-aware mediaID. We will need to know the media hierarchy
    // when we get a onPlayFromMusicID call, so we can create the proper queue based
    // on where the music was selected from (by artist, by genre, random, etc)
    String genre = metadata.getString(MediaMetadataCompat.METADATA_KEY_GENRE);
    String hierarchyAwareMediaID = MediaIDHelper.createMediaID(
            metadata.getDescription().getMediaId(), MEDIA_ID_MUSICS_BY_GENRE, genre);
    MediaMetadataCompat copy = new MediaMetadataCompat.Builder(metadata)
            .putString(MediaMetadataCompat.METADATA_KEY_MEDIA_ID, hierarchyAwareMediaID)
            .build();
    return new MediaBrowserCompat.MediaItem(copy.getDescription(),
            MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);

}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:16,
代码来源:MusicProvider.java

示例14: updateTitle

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
private void updateTitle() {
    if (MediaIDHelper.MEDIA_ID_ROOT.equals(mMediaId)) {
        mMediaFragmentListener.setToolbarTitle(null);
        return;
    }

    MediaBrowserCompat mediaBrowser = mMediaFragmentListener.getMediaBrowser();
    mediaBrowser.getItem(mMediaId, new MediaBrowserCompat.ItemCallback() {
        @Override
        public void onItemLoaded(MediaBrowserCompat.MediaItem item) {
            mMediaFragmentListener.setToolbarTitle(
                    item.getDescription().getTitle());
        }
    });
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:16,
代码来源:MediaBrowserFragment.java

示例15: getMediaItemState

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
public static int getMediaItemState(Activity context, MediaBrowserCompat.MediaItem mediaItem) {
    int state = STATE_NONE;
    // Set state to playable first, then override to playing or paused state if needed
    if (mediaItem.isPlayable()) {
        state = STATE_PLAYABLE;
        if (MediaIDHelper.isMediaItemPlaying(context, mediaItem)) {
            state = getStateFromController(context);
        }
    }

    return state;
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:13,
代码来源:MediaItemViewHolder.java

示例16: isSameBrowsingCategory

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
public boolean isSameBrowsingCategory(@NonNull String mediaId) {
    String[] newBrowseHierarchy = MediaIDHelper.getHierarchy(mediaId);
    MediaSessionCompat.QueueItem current = getCurrentMusic();
    if (current == null) {
        return false;
    }
    String[] currentBrowseHierarchy = MediaIDHelper.getHierarchy(
            current.getDescription().getMediaId());

    return Arrays.equals(newBrowseHierarchy, currentBrowseHierarchy);
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:12,
代码来源:QueueManager.java

示例17: loadMedia

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
private void loadMedia(String mediaId, boolean autoPlay) throws JSONException {
    String musicId = MediaIDHelper.extractMusicIDFromMediaID(mediaId);
    MediaMetadataCompat track = mMusicProvider.getMusic(musicId);
    if (track == null) {
        throw new IllegalArgumentException("Invalid mediaId " + mediaId);
    }
    if (!TextUtils.equals(mediaId, mCurrentMediaId)) {
        mCurrentMediaId = mediaId;
        mCurrentPosition = 0;
    }
    JSONObject customData = new JSONObject();
    customData.put(ITEM_ID, mediaId);
    MediaInfo media = toCastMediaMetadata(track, customData);
    mRemoteMediaClient.load(media, autoPlay, mCurrentPosition, customData);
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:16,
代码来源:CastPlayback.java

示例18: testSetQueueFromMusic

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Test
public void testSetQueueFromMusic() throws Exception {
    QueueManager queueManager = createQueueManagerWithValidation(null, -1, null);
    // get the first music of the first genre and build a hierarchy-aware version of its
    // mediaId
    String genre = provider.getGenres().iterator().next();
    MediaMetadataCompat metadata = provider.getMusicsByGenre(genre).iterator().next();
    String hierarchyAwareMediaID = MediaIDHelper.createMediaID(
            metadata.getDescription().getMediaId(), MediaIDHelper.MEDIA_ID_MUSICS_BY_GENRE,
            genre);

    // set a queue from the hierarchyAwareMediaID. It should contain all music with the same
    // genre
    queueManager.setQueueFromMusic(hierarchyAwareMediaID);

    // count all songs with the same genre
    int count = 0;
    for (MediaMetadataCompat m: provider.getMusicsByGenre(genre)) {
        count++;
    }

    // check if size matches
    assertEquals(count, queueManager.getCurrentQueueSize());

    // Now check if all songs in current queue have the expected genre:
    for (int i=0; i < queueManager.getCurrentQueueSize(); i++) {
        MediaSessionCompat.QueueItem item = queueManager.getCurrentMusic();
        String musicId = MediaIDHelper.extractMusicIDFromMediaID(
                item.getDescription().getMediaId());
        String itemGenre = provider.getMusic(musicId).getString(
                MediaMetadataCompat.METADATA_KEY_GENRE);
        assertEquals(genre, itemGenre);
        queueManager.skipQueuePosition(1);
    }
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:36,
代码来源:QueueManagerTest.java

示例19: play

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@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 == PlaybackState.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) {
        configMediaPlayerState();
    } else {
        mState = PlaybackState.STATE_STOPPED;
        relaxResources(false); // release everything except MediaPlayer
        MediaMetadata track = mMusicProvider.getMusic(
                MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        String source = track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE);

        try {
            createMediaPlayerIfNeeded();

            mState = PlaybackState.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());
            }
        }
    }
}
 

开发者ID:mrinalgit-dev,
项目名称:MrinalMusicPlayer,
代码行数:55,
代码来源:LocalPlayback.java

示例20: play

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@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 == PlaybackState.STATE_PAUSED && !mediaHasChanged && mMediaPlayer != null) {
        configMediaPlayerState();
    } else {
        mState = PlaybackState.STATE_STOPPED;
        relaxResources(false); // release everything except MediaPlayer
        MediaMetadataCompat track = mMusicProvider.getMusic(
                MediaIDHelper.extractMusicIDFromMediaID(item.getDescription().getMediaId()));

        String source = track.getString(MusicProvider.CUSTOM_METADATA_TRACK_SOURCE);

        try {
            createMediaPlayerIfNeeded();

            mState = PlaybackState.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());
            }
        }
    }
}
 

开发者ID:SoumyaParida,
项目名称:MyGaana-Universal,
代码行数:55,
代码来源:LocalPlayback.java

示例21: play

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Override
public void play(QueueItem item) {
    mPlayOnFocusGain = true;
    tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = item.getDescription().getMediaId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mCurrentMediaId = mediaId;
    }

    if (mediaHasChanged || mExoPlayer == null) {
        releaseResources(false); // release everything except the player
        MediaMetadataCompat track =
                mMusicProvider.getMusic(
                        MediaIDHelper.extractMusicIDFromMediaID(
                                item.getDescription().getMediaId()));

        String source = track.getString(MusicProviderSource.CUSTOM_METADATA_TRACK_SOURCE);
        if (source != null) {
            source = source.replaceAll(" ", "%20"); // Escape spaces for URLs
        }

        if (mExoPlayer == null) {
            mExoPlayer =
                    ExoPlayerFactory.newSimpleInstance(
                            mContext, new DefaultTrackSelector(), new DefaultLoadControl());
            mExoPlayer.addListener(mEventListener);
        }

        // Android "O" makes much greater use of AudioAttributes, especially
        // with regards to AudioFocus. All of UAMP's tracks are music, but
        // if your content includes spoken word such as audiobooks or podcasts
        // then the content type should be set to CONTENT_TYPE_SPEECH for those
        // tracks.
        final AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setContentType(CONTENT_TYPE_MUSIC)
                .setUsage(USAGE_MEDIA)
                .build();
        mExoPlayer.setAudioAttributes(audioAttributes);

        // Produces DataSource instances through which media data is loaded.
        DataSource.Factory dataSourceFactory =
                new DefaultDataSourceFactory(
                        mContext, Util.getUserAgent(mContext, "uamp"), null);
        // Produces Extractor instances for parsing the media data.
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        // The MediaSource represents the media to be played.
        MediaSource mediaSource =
                new ExtractorMediaSource(
                        Uri.parse(source), dataSourceFactory, extractorsFactory, null, null);

        // Prepares media to play (happens on background thread) and triggers
        // {@code onPlayerStateChanged} callback when the stream is ready to play.
        mExoPlayer.prepare(mediaSource);

        // 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();
    }

    configurePlayerState();
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:65,
代码来源:LocalPlayback.java

示例22: testPlay

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Test
public void testPlay() throws Exception {
    String mediaId = MediaIDHelper.MEDIA_ID_ROOT;
    while (MediaIDHelper.isBrowseable(mediaId)) {
        mediaId = musicProvider.getChildren(mediaId, resources).get(0).getMediaId();
    }

    // Using a CountDownLatch, we will check if all callbacks are called correctly when
    // a onPlayFromMediaId command is issued.
    final CountDownLatch latch = new CountDownLatch(5);
    final String expectedMediaId = mediaId;

    QueueManager queueManager = new QueueManager(musicProvider, resources, new SimpleMetadataUpdateListener(){
        @Override
        public void onMetadataChanged(MediaMetadataCompat metadata) {
            // Latch countdown 1: QueueManager will change appropriately
            assertEquals(MediaIDHelper.extractMusicIDFromMediaID(expectedMediaId),
                    metadata.getDescription().getMediaId());
            latch.countDown();
        }
    });

    SimplePlaybackServiceCallback serviceCallback = new SimplePlaybackServiceCallback() {
        @Override
        public void onPlaybackStart() {
            // Latch countdown 2: PlaybackService will get a onPlaybackStart call
            latch.countDown();
        }

        @Override
        public void onPlaybackStateUpdated(PlaybackStateCompat newState) {
            if (newState.getState() == PlaybackStateCompat.STATE_PLAYING) {
                // Latch countdown 3: PlaybackService will get a state updated call (here we
                // ignore the unrelated state changes)
                latch.countDown();
            }
        }

        @Override
        public void onNotificationRequired() {
            // Latch countdown 4: PlaybackService will get call to show a media notification
            latch.countDown();
        }
    };

    Playback playback = new SimplePlayback() {
        @Override
        public void play(MediaSessionCompat.QueueItem item) {
            // Latch countdown 5: Playback will be called with the correct queueItem
            assertEquals(expectedMediaId, item.getDescription().getMediaId());
            latch.countDown();
        }
    };

    PlaybackManager playbackManager = new PlaybackManager(serviceCallback, resources,
            musicProvider, queueManager, playback);
    playbackManager.getMediaSessionCallback().onPlayFromMediaId(expectedMediaId, null);

    latch.await(5, TimeUnit.SECONDS);

    // Finally, check if the current music in queueManager is as expected
    assertEquals(expectedMediaId, queueManager.getCurrentMusic().getDescription().getMediaId());
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:64,
代码来源:PlaybackManagerTest.java

示例23: testPlayFromSearch

点赞 2

import com.example.android.uamp.utils.MediaIDHelper; //导入依赖的package包/类
@Test
public void testPlayFromSearch() throws Exception {
    // Using a CountDownLatch, we will check if all callbacks are called correctly when
    // a onPlayFromMediaId command is issued.
    final CountDownLatch latch = new CountDownLatch(5);
    final String expectedMusicId = musicProvider.searchMusicBySongTitle("Music 3")
            .iterator().next().getDescription().getMediaId();

    QueueManager queueManager = new QueueManager(musicProvider, resources, new SimpleMetadataUpdateListener(){
        @Override
        public void onMetadataChanged(MediaMetadataCompat metadata) {
            // Latch countdown 1: QueueManager will change appropriately
            assertEquals(expectedMusicId, metadata.getDescription().getMediaId());
            latch.countDown();
        }
    });

    SimplePlaybackServiceCallback serviceCallback = new SimplePlaybackServiceCallback() {
        @Override
        public void onPlaybackStart() {
            // Latch countdown 2: PlaybackService will get a onPlaybackStart call
            latch.countDown();
        }

        @Override
        public void onPlaybackStateUpdated(PlaybackStateCompat newState) {
            if (newState.getState() == PlaybackStateCompat.STATE_PLAYING) {
                // Latch countdown 3: PlaybackService will get a state updated call (here we
                // ignore the unrelated state changes)
                latch.countDown();
            }
        }

        @Override
        public void onNotificationRequired() {
            // Latch countdown 4: PlaybackService will get call to show a media notification
            latch.countDown();
        }
    };

    Playback playback = new SimplePlayback() {
        @Override
        public void play(MediaSessionCompat.QueueItem item) {
            // Latch countdown 5: Playback will be called with the correct queueItem
            assertEquals(expectedMusicId, MediaIDHelper.extractMusicIDFromMediaID(
                    item.getDescription().getMediaId()));
            latch.countDown();
        }
    };

    PlaybackManager playbackManager = new PlaybackManager(serviceCallback, resources,
            musicProvider, queueManager, playback);
    playbackManager.getMediaSessionCallback().onPlayFromSearch("Music 3", null);

    latch.await(5, TimeUnit.SECONDS);

    // Finally, check if the current music in queueManager is as expected
    assertEquals(expectedMusicId,  MediaIDHelper.extractMusicIDFromMediaID(
            queueManager.getCurrentMusic().getDescription().getMediaId()));
}
 

开发者ID:googlesamples,
项目名称:android-UniversalMusicPlayer,
代码行数:61,
代码来源:PlaybackManagerTest.java


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)