Search in sources :

Example 6 with ExtractorMediaSource

use of com.google.android.exoplayer2.source.ExtractorMediaSource in project NiceMusic by lizixian18.

the class ExoPlayback method play.

@Override
public void play(SongInfo info) {
    mPlayOnFocusGain = true;
    mFocusAndLockManager.tryToGetAudioFocus();
    registerAudioNoisyReceiver();
    String mediaId = info.getSongId();
    boolean mediaHasChanged = !TextUtils.equals(mediaId, mCurrentMediaId);
    if (mediaHasChanged) {
        mCurrentMediaId = mediaId;
        mCurrentMediaSongInfo = info;
    }
    if (mediaHasChanged || mExoPlayer == null) {
        // release everything except the player
        releaseResources(false);
        String source = info.getSongUrl();
        if (source != null) {
            // Escape spaces for URLs
            source = source.replaceAll(" ", "%20");
        }
        if (mExoPlayer == null) {
            mExoPlayer = ExoPlayerFactory.newSimpleInstance(mContext, new DefaultTrackSelector(), new DefaultLoadControl());
            mExoPlayer.addListener(mEventListener);
        }
        final AudioAttributes audioAttributes = new AudioAttributes.Builder().setContentType(CONTENT_TYPE_MUSIC).setUsage(USAGE_MEDIA).build();
        mExoPlayer.setAudioAttributes(audioAttributes);
        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(mContext, Util.getUserAgent(mContext, "musiclibrary"), null);
        ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
        // MediaSource mediaSource = new ExtractorMediaSource(Uri.parse(source), dataSourceFactory, extractorsFactory, null, null);
        MediaSource mediaSource = buildMediaSource(Uri.parse(source), null, null, null);
        mExoPlayer.prepare(mediaSource);
        mFocusAndLockManager.acquireWifiLock();
    }
    configurePlayerState();
}
Also used : DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) HlsMediaSource(com.google.android.exoplayer2.source.hls.HlsMediaSource) DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) SsMediaSource(com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource) ExtractorsFactory(com.google.android.exoplayer2.extractor.ExtractorsFactory) DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) AudioAttributes(com.google.android.exoplayer2.audio.AudioAttributes) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) DataSource(com.google.android.exoplayer2.upstream.DataSource)

Example 7 with ExtractorMediaSource

use of com.google.android.exoplayer2.source.ExtractorMediaSource in project Camera-Roll-Android-App by kollerlukas.

the class VideoPlayerActivity method initPlayer.

private void initPlayer() {
    // Produces DataSource instances through which media data is loaded.
    DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, Util.getUserAgent(this, getString(R.string.app_name)), null);
    // Produces Extractor instances for parsing the media data.
    ExtractorsFactory extractorsFactory = new DefaultExtractorsFactory();
    // This is the MediaSource representing the media to be played.
    MediaSource videoSource = new ExtractorMediaSource(videoUri, dataSourceFactory, extractorsFactory, null, null);
    DefaultRenderersFactory renderersFactory = new DefaultRenderersFactory(this);
    // Create the player
    player = ExoPlayerFactory.newSimpleInstance(renderersFactory, new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(null)), new DefaultLoadControl());
    // Bind the player to the view.
    SimpleExoPlayerView simpleExoPlayerView = findViewById(R.id.simpleExoPlayerView);
    simpleExoPlayerView.setPlayer(player);
    // Prepare the player with the source.
    player.prepare(videoSource);
    player.setRepeatMode(Player.REPEAT_MODE_ONE);
    player.setPlayWhenReady(true);
    final ImageButton playPause = findViewById(R.id.play_pause);
    player.addListener(new SimpleEventListener() {

        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
            // update PlayPause-Button
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP && showAnimations()) {
                if (player.getPlayWhenReady()) {
                    playPause.setImageResource(R.drawable.play_to_pause_avd);
                } else {
                    playPause.setImageResource(R.drawable.pause_to_play_avd);
                }
                Drawable d = playPause.getDrawable();
                if (d instanceof Animatable) {
                    ((Animatable) d).start();
                }
            } else {
                if (player.getPlayWhenReady()) {
                    playPause.setImageResource(R.drawable.ic_pause_white);
                } else {
                    playPause.setImageResource(R.drawable.ic_play_arrow_white);
                }
            }
        }
    });
}
Also used : DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) Drawable(android.graphics.drawable.Drawable) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) DataSource(com.google.android.exoplayer2.upstream.DataSource) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) ImageButton(android.widget.ImageButton) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) ExtractorsFactory(com.google.android.exoplayer2.extractor.ExtractorsFactory) DefaultExtractorsFactory(com.google.android.exoplayer2.extractor.DefaultExtractorsFactory) DefaultDataSourceFactory(com.google.android.exoplayer2.upstream.DefaultDataSourceFactory) AdaptiveTrackSelection(com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection) SimpleExoPlayerView(com.google.android.exoplayer2.ui.SimpleExoPlayerView) Animatable(android.graphics.drawable.Animatable) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) DefaultRenderersFactory(com.google.android.exoplayer2.DefaultRenderersFactory)

Aggregations

ExtractorMediaSource (com.google.android.exoplayer2.source.ExtractorMediaSource)7 MediaSource (com.google.android.exoplayer2.source.MediaSource)7 DefaultDataSourceFactory (com.google.android.exoplayer2.upstream.DefaultDataSourceFactory)7 DefaultExtractorsFactory (com.google.android.exoplayer2.extractor.DefaultExtractorsFactory)6 ExtractorsFactory (com.google.android.exoplayer2.extractor.ExtractorsFactory)6 DataSource (com.google.android.exoplayer2.upstream.DataSource)6 DefaultTrackSelector (com.google.android.exoplayer2.trackselection.DefaultTrackSelector)5 DefaultLoadControl (com.google.android.exoplayer2.DefaultLoadControl)4 AudioAttributes (com.google.android.exoplayer2.audio.AudioAttributes)3 DashMediaSource (com.google.android.exoplayer2.source.dash.DashMediaSource)2 HlsMediaSource (com.google.android.exoplayer2.source.hls.HlsMediaSource)2 SsMediaSource (com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource)2 AdaptiveTrackSelection (com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection)2 DefaultHttpDataSource (com.google.android.exoplayer2.upstream.DefaultHttpDataSource)2 DefaultHttpDataSourceFactory (com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory)2 Animatable (android.graphics.drawable.Animatable)1 Drawable (android.graphics.drawable.Drawable)1 Uri (android.net.Uri)1 MediaMetadataCompat (android.support.v4.media.MediaMetadataCompat)1 ImageButton (android.widget.ImageButton)1