Search in sources :

Example 6 with DefaultRenderersFactory

use of com.google.android.exoplayer2.DefaultRenderersFactory in project PreviewSeekBar by rubensousa.

the class ExoPlayerManager method createFullPlayer.

private SimpleExoPlayer createFullPlayer() {
    TrackSelection.Factory videoTrackSelectionFactory = new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter());
    TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
    LoadControl loadControl = new DefaultLoadControl();
    SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(playerView.getContext()), trackSelector, loadControl);
    player.setPlayWhenReady(true);
    player.prepare(mediaSourceBuilder.getMediaSource(false));
    player.addListener(eventListener);
    return player;
}
Also used : SimpleExoPlayer(com.google.android.exoplayer2.SimpleExoPlayer) DefaultBandwidthMeter(com.google.android.exoplayer2.upstream.DefaultBandwidthMeter) LoadControl(com.google.android.exoplayer2.LoadControl) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) TrackSelector(com.google.android.exoplayer2.trackselection.TrackSelector) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) ExoPlayerFactory(com.google.android.exoplayer2.ExoPlayerFactory) DefaultRenderersFactory(com.google.android.exoplayer2.DefaultRenderersFactory) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) DefaultRenderersFactory(com.google.android.exoplayer2.DefaultRenderersFactory) TrackSelection(com.google.android.exoplayer2.trackselection.TrackSelection) AdaptiveTrackSelection(com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl)

Example 7 with DefaultRenderersFactory

use of com.google.android.exoplayer2.DefaultRenderersFactory in project twicalico by moko256.

the class ImagePagerChildFragment method onCreateView.

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    FlingLayout view = (FlingLayout) inflater.inflate(R.layout.fragment_image_pager_child, null);
    view.setDismissListener(() -> {
        getActivity().finish();
        return Unit.INSTANCE;
    });
    view.setPositionChangeListener((Integer top, Integer left, Float dragRangeRate) -> {
        view.setBackgroundColor(Color.argb(Math.round(255 * (1.0F - dragRangeRate)), 0, 0, 0));
        return Unit.INSTANCE;
    });
    MediaEntity mediaEntity;
    if (getArguments() == null)
        return view;
    mediaEntity = (MediaEntity) getArguments().getSerializable(FRAG_MEDIA_ENTITY);
    if (mediaEntity == null)
        return view;
    switch(mediaEntity.getType()) {
        case "video":
            String videoPath = null;
            boolean isHls = false;
            for (MediaEntity.Variant variant : mediaEntity.getVideoVariants()) {
                if (variant.getContentType().equals("application/x-mpegURL")) {
                    videoPath = variant.getUrl();
                    isHls = true;
                }
            }
            if (videoPath == null) {
                videoPath = mediaEntity.getVideoVariants()[0].getUrl();
            }
            videoPlayView = view.findViewById(R.id.fragment_image_pager_video);
            videoPlayView.setVisibility(View.VISIBLE);
            videoPlayView.setControllerVisibilityListener(visibility -> {
                if (visibility != View.VISIBLE) {
                    hideSystemUI();
                } else {
                    showSystemUI();
                }
            });
            getActivity().getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    ((AppCompatActivity) getActivity()).getSupportActionBar().show();
                    videoPlayView.showController();
                }
            });
            player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(getContext()), new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter(), Integer.MAX_VALUE, AdaptiveTrackSelection.DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS, AdaptiveTrackSelection.DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS, AdaptiveTrackSelection.DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS, AdaptiveTrackSelection.DEFAULT_BANDWIDTH_FRACTION)), new DefaultLoadControl());
            if (savedInstanceState != null) {
                player.seekTo(savedInstanceState.getLong("video_time", 0));
            }
            videoPlayView.setPlayer(player);
            player.prepare((isHls) ? new HlsMediaSource.Factory(new OkHttpDataSourceFactory(GlobalApplication.getOkHttpClient(), getResources().getText(R.string.app_name).toString(), null)).createMediaSource(Uri.parse(videoPath)) : new ExtractorMediaSource.Factory(new OkHttpDataSourceFactory(GlobalApplication.getOkHttpClient(), getResources().getText(R.string.app_name).toString(), null)).createMediaSource(Uri.parse(mediaEntity.getVideoVariants()[0].getUrl()), new Handler(), null));
            break;
        case "animated_gif":
            videoPlayView = view.findViewById(R.id.fragment_image_pager_video);
            videoPlayView.setVisibility(View.VISIBLE);
            videoPlayView.setControllerVisibilityListener(visibility -> {
                if (visibility != View.VISIBLE) {
                    hideSystemUI();
                } else {
                    showSystemUI();
                }
            });
            getActivity().getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    ((AppCompatActivity) getActivity()).getSupportActionBar().show();
                    videoPlayView.showController();
                }
            });
            player = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(getContext()), new DefaultTrackSelector(new AdaptiveTrackSelection.Factory(new DefaultBandwidthMeter(), Integer.MAX_VALUE, AdaptiveTrackSelection.DEFAULT_MIN_DURATION_FOR_QUALITY_INCREASE_MS, AdaptiveTrackSelection.DEFAULT_MAX_DURATION_FOR_QUALITY_DECREASE_MS, AdaptiveTrackSelection.DEFAULT_MIN_DURATION_TO_RETAIN_AFTER_DISCARD_MS, AdaptiveTrackSelection.DEFAULT_BANDWIDTH_FRACTION)), new DefaultLoadControl());
            if (savedInstanceState != null) {
                player.seekTo(savedInstanceState.getLong("video_time", 0));
            }
            videoPlayView.setPlayer(player);
            player.prepare(new LoopingMediaSource(new ExtractorMediaSource.Factory(new OkHttpDataSourceFactory(GlobalApplication.getOkHttpClient(), getResources().getText(R.string.app_name).toString(), null)).createMediaSource(Uri.parse(mediaEntity.getVideoVariants()[0].getUrl()), new Handler(), null)));
            player.setPlayWhenReady(true);
            break;
        case "photo":
        default:
            getActivity().getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(visibility -> {
                if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    ((AppCompatActivity) getActivity()).getSupportActionBar().show();
                }
            });
            imageView = view.findViewById(R.id.fragment_image_pager_image);
            imageView.setVisibility(View.VISIBLE);
            imageView.setOnClickListener(v -> {
                if ((getActivity().getWindow().getDecorView().getSystemUiVisibility() & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                    hideSystemUI();
                } else {
                    showSystemUI();
                }
            });
            imageView.setOnScaleChangeListener((float scaleFactor, float focusX, float focusY) -> view.setDragEnabled(scaleFactor <= 1F));
            GlideApp.with(this).load(TwitterStringUtils.convertLargeImageUrl(mediaEntity.getMediaURLHttps())).fitCenter().thumbnail(GlideApp.with(this).load(TwitterStringUtils.convertSmallImageUrl(mediaEntity.getMediaURLHttps())).fitCenter()).into(imageView);
            break;
    }
    return view;
}
Also used : DefaultBandwidthMeter(com.google.android.exoplayer2.upstream.DefaultBandwidthMeter) Handler(android.os.Handler) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) HlsMediaSource(com.google.android.exoplayer2.source.hls.HlsMediaSource) AdaptiveTrackSelection(com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) OkHttpDataSourceFactory(com.google.android.exoplayer2.ext.okhttp.OkHttpDataSourceFactory) DefaultRenderersFactory(com.google.android.exoplayer2.DefaultRenderersFactory) MediaEntity(twitter4j.MediaEntity) FlingLayout(com.github.chuross.flinglayout.FlingLayout) LoopingMediaSource(com.google.android.exoplayer2.source.LoopingMediaSource) Nullable(android.support.annotation.Nullable)

Example 8 with DefaultRenderersFactory

use of com.google.android.exoplayer2.DefaultRenderersFactory 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)

Example 9 with DefaultRenderersFactory

use of com.google.android.exoplayer2.DefaultRenderersFactory in project MusicLibrary 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 (TextUtils.isEmpty(source)) {
            if (mCallback != null) {
                mCallback.onError("song url is null");
            }
            return;
        }
        Uri playUri;
        if (BaseUtil.isOnLineSource(source)) {
            String proxyUrl;
            if (isOpenCacheWhenPlaying && (getMediaType(null, Uri.parse(source)) == C.TYPE_OTHER)) {
                proxyUrl = mProxyCacheServer.getProxyUrl(source);
            } else {
                proxyUrl = source;
            }
            playUri = Uri.parse(proxyUrl);
        } else {
            playUri = BaseUtil.getLocalSourceUri(source);
        }
        if (playUri == null) {
            if (mCallback != null) {
                mCallback.onError("song uri is null");
            }
            return;
        }
        LogUtil.i("isOpenCacheWhenPlaying = " + isOpenCacheWhenPlaying + " playUri = " + playUri.toString());
        if (mExoPlayer == null) {
            mExoPlayer = ExoPlayerFactory.newSimpleInstance(new DefaultRenderersFactory(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);
        MediaSource mediaSource = buildMediaSource(playUri, null, null, null);
        mExoPlayer.prepare(mediaSource);
        mFocusAndLockManager.acquireWifiLock();
    }
    configurePlayerState();
}
Also used : DashMediaSource(com.google.android.exoplayer2.source.dash.DashMediaSource) MediaSource(com.google.android.exoplayer2.source.MediaSource) SsMediaSource(com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource) HlsMediaSource(com.google.android.exoplayer2.source.hls.HlsMediaSource) ExtractorMediaSource(com.google.android.exoplayer2.source.ExtractorMediaSource) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) AudioAttributes(com.google.android.exoplayer2.audio.AudioAttributes) DefaultRenderersFactory(com.google.android.exoplayer2.DefaultRenderersFactory) Uri(android.net.Uri) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl)

Example 10 with DefaultRenderersFactory

use of com.google.android.exoplayer2.DefaultRenderersFactory in project AntennaPod by AntennaPod.

the class ExoPlayerWrapper method createPlayer.

private void createPlayer() {
    DefaultLoadControl.Builder loadControl = new DefaultLoadControl.Builder();
    loadControl.setBufferDurationsMs(30000, 120000, DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS, DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS);
    loadControl.setBackBuffer(UserPreferences.getRewindSecs() * 1000 + 500, true);
    trackSelector = new DefaultTrackSelector(context);
    exoPlayer = new SimpleExoPlayer.Builder(context, new DefaultRenderersFactory(context)).setTrackSelector(trackSelector).setLoadControl(loadControl.build()).build();
    exoPlayer.setSeekParameters(SeekParameters.EXACT);
    exoPlayer.addListener(new Player.Listener() {

        @Override
        public void onPlaybackStateChanged(@Player.State int playbackState) {
            if (audioCompletionListener != null && playbackState == Player.STATE_ENDED) {
                audioCompletionListener.onCompletion(null);
            } else if (infoListener != null && playbackState == Player.STATE_BUFFERING) {
                infoListener.onInfo(null, android.media.MediaPlayer.MEDIA_INFO_BUFFERING_START, 0);
            } else if (infoListener != null) {
                infoListener.onInfo(null, android.media.MediaPlayer.MEDIA_INFO_BUFFERING_END, 0);
            }
        }

        @Override
        public void onPlayerError(@NonNull ExoPlaybackException error) {
            if (audioErrorListener != null) {
                if (NetworkUtils.wasDownloadBlocked(error)) {
                    audioErrorListener.accept(context.getString(R.string.download_error_blocked));
                } else {
                    Throwable cause = error.getCause();
                    if (cause instanceof HttpDataSource.HttpDataSourceException) {
                        if (cause.getCause() != null) {
                            cause = cause.getCause();
                        }
                    }
                    if (cause != null && "Source error".equals(cause.getMessage())) {
                        cause = cause.getCause();
                    }
                    audioErrorListener.accept(cause != null ? cause.getMessage() : error.getMessage());
                }
            }
        }

        @Override
        public void onPositionDiscontinuity(@NonNull Player.PositionInfo oldPosition, @NonNull Player.PositionInfo newPosition, @Player.DiscontinuityReason int reason) {
            if (audioSeekCompleteListener != null && reason == Player.DISCONTINUITY_REASON_SEEK) {
                audioSeekCompleteListener.onSeekComplete(null);
            }
        }
    });
}
Also used : Player(com.google.android.exoplayer2.Player) SimpleExoPlayer(com.google.android.exoplayer2.SimpleExoPlayer) PlaybackServiceMediaPlayer(de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer) IPlayer(de.danoeh.antennapod.core.util.playback.IPlayer) MediaPlayer(org.antennapod.audio.MediaPlayer) ExoPlaybackException(com.google.android.exoplayer2.ExoPlaybackException) DefaultLoadControl(com.google.android.exoplayer2.DefaultLoadControl) DefaultTrackSelector(com.google.android.exoplayer2.trackselection.DefaultTrackSelector) OkHttpDataSource(com.google.android.exoplayer2.ext.okhttp.OkHttpDataSource) HttpDataSource(com.google.android.exoplayer2.upstream.HttpDataSource) DefaultRenderersFactory(com.google.android.exoplayer2.DefaultRenderersFactory)

Aggregations

DefaultRenderersFactory (com.google.android.exoplayer2.DefaultRenderersFactory)10 DefaultTrackSelector (com.google.android.exoplayer2.trackselection.DefaultTrackSelector)8 DefaultLoadControl (com.google.android.exoplayer2.DefaultLoadControl)7 AdaptiveTrackSelection (com.google.android.exoplayer2.trackselection.AdaptiveTrackSelection)5 ExtractorMediaSource (com.google.android.exoplayer2.source.ExtractorMediaSource)4 ExoPlayerFactory (com.google.android.exoplayer2.ExoPlayerFactory)3 DefaultExtractorsFactory (com.google.android.exoplayer2.extractor.DefaultExtractorsFactory)3 MediaSource (com.google.android.exoplayer2.source.MediaSource)3 HlsMediaSource (com.google.android.exoplayer2.source.hls.HlsMediaSource)3 DefaultBandwidthMeter (com.google.android.exoplayer2.upstream.DefaultBandwidthMeter)3 DefaultDataSourceFactory (com.google.android.exoplayer2.upstream.DefaultDataSourceFactory)3 Intent (android.content.Intent)2 Uri (android.net.Uri)2 LoadControl (com.google.android.exoplayer2.LoadControl)2 SimpleExoPlayer (com.google.android.exoplayer2.SimpleExoPlayer)2 OkHttpDataSourceFactory (com.google.android.exoplayer2.ext.okhttp.OkHttpDataSourceFactory)2 DashMediaSource (com.google.android.exoplayer2.source.dash.DashMediaSource)2 SsMediaSource (com.google.android.exoplayer2.source.smoothstreaming.SsMediaSource)2 TrackSelection (com.google.android.exoplayer2.trackselection.TrackSelection)2 TrackSelector (com.google.android.exoplayer2.trackselection.TrackSelector)2