当前位置:   article > 正文

PreviewSeekBar_exo_simple_player_view

exo_simple_player_view

PreviewSeekBar

项目地址: rubensousa/PreviewSeekBar
简介:A SeekBar suited for showing a preview of something. As seen in Google Play Movies.
更多: 作者    提 Bug   
标签:

A SeekBar suited for showing a preview of something. As seen in Google Play Movies.

Google Play Movies

PreviewSeekBar's sample

Build

Add the following to your app's build.gradle:

  1. dependencies {
  2. compile 'com.github.rubensousa:previewseekbar:1.0'
  3. // If you want to use this with ExoPlayer, use this one:
  4. compile 'com.github.rubensousa:previewseekbar-exoplayer:2.5.1'
  5. }

How to use

Add the following XML:
  1. <com.github.rubensousa.previewseekbar.PreviewSeekBarLayout
  2. android:id="@+id/previewSeekBarLayout"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical">
  6. <FrameLayout
  7. android:id="@+id/previewFrameLayout"
  8. android:layout_width="@dimen/video_preview_width"
  9. android:layout_height="@dimen/video_preview_height">
  10. <View
  11. android:id="@+id/videoView"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent"
  14. android:layout_gravity="start"
  15. android:background="@color/colorPrimary" />
  16. </FrameLayout>
  17. <com.github.rubensousa.previewseekbar.PreviewSeekBar
  18. android:id="@+id/previewSeekBar"
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:layout_below="@id/previewFrameLayout"
  22. android:layout_marginTop="25dp"
  23. android:max="800" />
  24. </com.github.rubensousa.previewseekbar.PreviewSeekBarLayout>
You need to add at least one PreviewSeekBar and a FrameLayout inside PreviewSeekBarLayout, else an exception will be thrown.

PreviewSeekBarLayout extends from RelativeLayout so you can add other views or layouts there.

Create a PreviewLoader and pass it to PreviewSeekBarLayout:
  1. // Create a class that implements this interface and implement your own preview logic there
  2. public interface PreviewLoader {
  3. void loadPreview(long currentPosition, long max);
  4. }
  5. PreviewLoader loader = new ExoPlayerLoader();
  6. previewSeekBarLayout.setup(loader);

How to use with ExoPlayer

Add a custom controller to your SimpleExoPlayerView
  1. <com.google.android.exoplayer2.ui.SimpleExoPlayerView
  2. android:id="@+id/player_view"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. app:controller_layout_id="@layout/exoplayer_controls"/>

Here's the sample's exoplayer_controls: https://raw.githubusercontent.com/rubensousa/PreviewSeekBar/master/sample/src/main/res/layout/exoplayer_controls.xml

The PreviewSeekBarLayout inside exoplayer_controls should be similar to this:

  1. <com.github.rubensousa.previewseekbar.exoplayer.PreviewTimeBarLayout
  2. android:id="@+id/previewTimeBarLayout"
  3. android:layout_width="0dp"
  4. android:layout_height="wrap_content"
  5. android:layout_weight="1">
  6. <FrameLayout
  7. android:id="@+id/previewFrameLayout"
  8. android:layout_width="@dimen/video_preview_width"
  9. android:layout_height="@dimen/video_preview_height"
  10. android:background="@drawable/video_frame"
  11. android:padding="@dimen/video_frame_width">
  12. <com.google.android.exoplayer2.ui.SimpleExoPlayerView
  13. android:id="@+id/previewPlayerView"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. app:controller_layout_id="@layout/exo_simple_player_view"
  17. app:surface_type="texture_view"
  18. app:use_artwork="false"
  19. app:use_controller="false" />
  20. </FrameLayout>
  21. <com.github.rubensousa.previewseekbar.exoplayer.PreviewTimeBar
  22. android:id="@+id/exo_progress"
  23. style="?android:attr/progressBarStyleHorizontal"
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content"
  26. android:layout_below="@id/previewFrameLayout"
  27. android:layout_marginTop="10dp"
  28. android:max="800" />
  29. </com.github.rubensousa.previewseekbar.PreviewSeekBarLayout>
Use the following SimpleExoPlayerView for the preview:
  1. <com.google.android.exoplayer2.ui.SimpleExoPlayerView
  2. android:id="@+id/previewPlayerView"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. app:controller_layout_id="@layout/exo_simple_player_view"
  6. app:surface_type="texture_view"
  7. app:use_artwork="false"
  8. app:use_controller="false" />

We specify another controller layout because the default one includes a SeekBar with the same id as ours.

Create a player with a custom TrackSelection and LoadControl
  1. private SimpleExoPlayer createPreviewPlayer() {
  2. TrackSelection.Factory videoTrackSelectionFactory = new WorstVideoTrackSelection.Factory();
  3. TrackSelector trackSelector = new DefaultTrackSelector(videoTrackSelectionFactory);
  4. LoadControl loadControl = new PreviewLoadControl();
  5. SimpleExoPlayer player = ExoPlayerFactory.newSimpleInstance(
  6. new DefaultRenderersFactory(context), trackSelector, loadControl);
  7. player.setPlayWhenReady(false);
  8. player.setVolume(0f);
  9. player.prepare(mediaSourceBuilder.getMediaSource(true));
  10. return player;
  11. }

PreviewLoadControl and WorstVideoTrackSelection are already included in previewseekbar-exoplayer. Check the next section for some improvements notes.

Create your own ExoPlayerLoader that seeks the video to the current position
  1. @Override
  2. public void loadPreview(long currentPosition, long max) {
  3. previewPlayer.seekTo(currentPosition);
  4. previewPlayer.setPlayWhenReady(false);
  5. }

Improvements

The sample uses some code adapted from the ExoPlayer official demo: https://github.com/google/ExoPlayer

A few improvements would be:

  • Adding a stream with lower bitrate to load and display the images faster.

  • Using some kind of special stream just for the thumbnails. Maybe this is how the Google Play team did it, I don't know. They load the thumbnails a lot faster than this sample.

  • Caching thumbnails in disk for offline use.

Any ideas for improving this would be welcomed!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/295786
推荐阅读
相关标签
  

闽ICP备14008679号