Foggy day

Android(java) - exoplayer2 본문

Android

Android(java) - exoplayer2

jinhan38 2021. 3. 5. 21:33

 

 

1. exo_player_activity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.exoplayer2.ui.PlayerView
        android:id="@+id/playerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:controller_layout_id="@layout/custom_exoplayer_controller"
        app:resize_mode="fixed_width" />

</LinearLayout>

 

 

2. custom_exoplayer_controller

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#99000000">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageButton
            android:id="@+id/exo_back"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="?android:attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:src="@drawable/ic_arrow_left_white"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <ImageView
            android:id="@+id/exo_fullscreen_icon"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:adjustViewBounds="true"
            android:background="?android:attr/selectableItemBackgroundBorderless"
            android:padding="10dp"
            android:scaleType="center"
            android:src="@drawable/ic_fullscreen_open"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            android:id="@id/exo_play"
            style="@style/ExoMediaButton.Play"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/colorTransparent"
            android:padding="20dp"
            android:src="@drawable/icon_etc_play"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageButton
            android:id="@id/exo_pause"
            style="@style/ExoMediaButton.Pause"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:background="@color/colorTransparent"
            android:scaleType="center"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:orientation="horizontal"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent">

            <TextView
                android:id="@id/exo_position"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:includeFontPadding="false"
                android:paddingLeft="24dp"
                android:paddingRight="12dp"
                android:textColor="@color/white"
                android:textSize="11dp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />


            <com.google.android.exoplayer2.ui.DefaultTimeBar
                android:id="@id/exo_progress"
                android:layout_width="0dp"
                android:layout_height="26dp"
                android:layout_weight="1"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toStartOf="@+id/exo_duration"
                app:layout_constraintStart_toEndOf="@+id/exo_position"
                app:layout_constraintTop_toTopOf="parent" />

            <TextView
                android:id="@id/exo_duration"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:paddingLeft="12dp"
                android:paddingRight="24dp"
                android:textColor="@color/white"
                android:textSize="11dp"
                android:textStyle="bold"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </androidx.constraintlayout.widget.ConstraintLayout>

    </androidx.constraintlayout.widget.ConstraintLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

3. ExoPlayerActivity

public class ExoPlayerActivity extends AppCompatActivity {


    private ExoPlayerActivityBinding b;
    private ExoPlayer exoPlayer;
    private Context mContext;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        b = ExoPlayerActivityBinding.inflate(getLayoutInflater());
        setContentView(b.getRoot());
        mContext = this;

        initExoPlayer();
    }


    private void initExoPlayer() {

        exoPlayer = ExoPlayerFactory.newSimpleInstance(mContext);
        b.playerView.setPlayer(exoPlayer);
//        playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FIT);


        DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(getApplicationContext(),
                Util.getUserAgent(getApplicationContext(), getApplicationContext().getString(R.string.app_name)));

//        Uri video = Uri.parse("android.resource://" + getPackageName() + "/raw/sound_looping");
//        Log.d(TAG, "initExoPlayer: video : " + video);

//        MediaSource videoSource = new ProgressiveMediaSource.Factory(dataSourceFactory)
//                .createMediaSource(mediaSource); // 동영상 url

        String sample = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4";
        MediaSource mediaSource = buildMediaSource(Uri.parse(sample));


        exoPlayer.prepare(mediaSource);
        exoPlayer.setPlayWhenReady(false);
//        exoPlayer.seekTo(seekto);  //프로그레스바 이동
        exoPlayer.addListener(exoPlayerListener);

    }

    private MediaSource buildMediaSource(Uri uri) {

        String userAgent = Util.getUserAgent(this, "jinhan");

        return new ExtractorMediaSource.Factory(new DefaultHttpDataSourceFactory(userAgent))
                .createMediaSource(uri);
    }

    private Player.EventListener exoPlayerListener = new Player.EventListener() {
        @Override
        public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {

            // playWhenReady
            // true : playing
            // false : stop

            // playbackState
            //  1 : When Activity with ExoPlayer disappears
            //  2 : When state of seekBar changes
            //  3 :  ....   exoplayer가 있는 액티비티에 있는 상태

            /**
             * The player does not have any media to play.
             */
            int STATE_IDLE = 1;
            /**
             * The player is not able to immediately play from its current position. This state typically
             * occurs when more data needs to be loaded.
             */
            int STATE_BUFFERING = 2;
            /**
             * The player is able to immediately play from its current position. The player will be playing if
             * {@link #getPlayWhenReady()} is true, and paused otherwise.
             */
            int STATE_READY = 3;
            /**
             * The player has finished playing the media.
             */
            int STATE_ENDED = 4;


            if (playWhenReady) {
                Common.Companion.setToastGravityCenter(mContext, "play");
            } else {
                Common.Companion.setToastGravityCenter(mContext, "stop");
            }


            switch (playbackState) {
                case 1:

                    Common.Companion.setToastGravityCenter(mContext, "STATE_IDLE");
                    break;
                case 2:

                    Common.Companion.setToastGravityCenter(mContext, "STATE_BUFFERING");
                    break;
                case 3:

                    Common.Companion.setToastGravityCenter(mContext, "STATE_READY");
                    break;
                case 4:

                    Common.Companion.setToastGravityCenter(mContext, "STATE_ENDED");
                    break;
            }

        }
    };
}