경험의 기록

 

https://github.com/google/ExoPlayer

 

GitHub - google/ExoPlayer: An extensible media player for Android

An extensible media player for Android. Contribute to google/ExoPlayer development by creating an account on GitHub.

github.com

ExoPlayer를 사용하면 안드로이드에서 영상 파일을 재생할 수 있다.

 


Dependency 추가

exoplayer는 필요한 부분만 따로 Dependency를 추가할 수 있다.

 

이 글에서는 테스트를 위해 전체

implementation 'com.google.android.exoplayer:exoplayer:2.18.0'

추가해주었다.

 

Permission

<uses-permission android:name="android.permission.INTERNET" />

인터넷 경로의 파일을 받아오는 경우 인터넷 권한 또한 추가해준다.

 

 

layout

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

    </data>

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

        <com.google.android.exoplayer2.ui.PlayerView
            android:id="@+id/playerView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/black"
            app:auto_show="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.5"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:resize_mode="fixed_width"
            app:surface_type="surface_view"
            app:use_controller="true" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

 

PlayerView를 추가해준다.

속성은 이름을 통해 직관적으로 알 수 있으며

https://exoplayer.dev/doc/reference/com/google/android/exoplayer2/ui/PlayerView.html

 

PlayerView (ExoPlayer library)

Deprecated. Sets whether the currently displayed video frame or media artwork is kept visible when the player is reset. A player reset is defined to mean the player being re-prepared with different media, the player transitioning to unprepared media or an

exoplayer.dev

자세한 속성은 이곳에서 확인할 수 있다.

 

MainActivity

class MainActivity : AppCompatActivity() {

    private lateinit var binding: ActivityMainBinding
    private var simpleExoPlayer: SimpleExoPlayer? = null
    private var videoUrl = "http://techslides.com/demos/sample-videos/small.mp4"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        binding = DataBindingUtil.setContentView(this,R.layout.activity_main)
        binding.lifecycleOwner = this

        initPlayer()
    }

    private fun initPlayer(){
        simpleExoPlayer = SimpleExoPlayer.Builder(this).build()
        binding.playerView.player = simpleExoPlayer
        buildMediaSource()?.let {
            simpleExoPlayer?.prepare(it)
        }
    }

    // 영상에 출력할 미디어 정보를 가져오는 클래스
    private fun buildMediaSource(): MediaSource {
        val dataSourceFactory = DefaultDataSourceFactory(this, "sample")
        return ProgressiveMediaSource.Factory(dataSourceFactory)
            .createMediaSource(MediaItem.fromUri(videoUrl))
    }

    // 일시중지
    override fun onResume() {
        super.onResume()
        simpleExoPlayer?.playWhenReady = true
    }

    override fun onStop() {
        super.onStop()
        simpleExoPlayer?.stop()
        simpleExoPlayer?.playWhenReady = false
    }

    override fun onDestroy() {
        super.onDestroy()
        simpleExoPlayer?.release()
    }
}

이제 build()를 통해 플레이어를 빌드한다.

단, 빌드는 생명주기 대응하여 맞춰 진행해야 한다. 

이 글에서는 onCreate에서 빌드하였으므로 onDestory에서 release하여 빌드를 해제해주어야 한다.

 

빌드 후 buildMediaSource() 함수를 통해 내가 불러오고자 하는 동영상의 url를 연결해주고,

생명주기 (onResume, onStop) 에 맞춰 각각 처리해주면 된다.

 

 

 

 

전체 코드

https://github.com/HanYeop/AndroidStudio-Practice2/tree/master/ExoPlayerEx

 

GitHub - HanYeop/AndroidStudio-Practice2: (2021.05.20~) 안드로이드 학습 내용 저장소

(2021.05.20~) 안드로이드 학습 내용 저장소. Contribute to HanYeop/AndroidStudio-Practice2 development by creating an account on GitHub.

github.com

 

 

참고

https://homzzang.com/b/free-4555

 

반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band
loading