반응형
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- 회전 센서
- 금속 탐지기 센서
- Android
- 걸음수 감지 센서
- 동영상 실행
- Android Studio 3.6
- BottomSheetDialog
- powercfg
- 코틀린
- LayoutParmas
- 온라인IDE
- 온라인에디터
- lateinit
- Exoplayer
- phpstorm
- 온라인코딩
- utf8mb4
- 광 센서
- ARGB
- Kotlin
- RecyclerView
- 웹코드빌드
- 온라인무료코딩사이트
- mysql 5.7
- 자기장 센서
- 걸음 감지 센서
- setBackgroundResource
- Aplha
- 자격증
Archives
- Today
- Total
Memory
[ANDROID/KOTLIN] ExoPlayer로 동영상 실행하기 (r2.12.2) 본문
반응형
ExoPlayer 공식 홈페이지
exoplayer.dev/hello-world.html
XML
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.VideoDetailActivity">
<FrameLayout
android:id="@+id/frameL_video_detail"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 기본
빨리감기 / 되감기 : 15초
-->
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/exoPlayerV_video_detail"
android:layout_width="match_parent"
android:layout_height="250dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<!-- Custom
show_timeout : PlayerView사용자가 마지막으로 상호 작용 한 후 컨트롤이 숨겨지기 전까지의 지연 시간
빨리감기(fastforward_increment) / 되감기(rewind_increment) : 30초
-->
<com.google.android.exoplayer2.ui.PlayerView
android:id="@+id/exoPlayerV_video_detail"
android:layout_width="match_parent"
android:layout_height="250dp"
app:show_timeout="10000"
app:fastforward_increment="30000"
app:rewind_increment="30000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</FrameLayout>
...
</LinearLayout
CODE ( ViewBinding 사용중 )
class VideoDetailActivity : AppCompatActivity() {
private lateinit var mBinding: ActivityVideoDetailBinding
private var player: SimpleExoPlayer? = null
private var playWhenReady = true
private var currentWindow = 0
private var playbackPosition = 0L
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = ActivityVideoDetailBinding.inflate(layoutInflater)
setContentView(mBinding.root)
mContext = this@VideoDetailActivity
}
fun initializePlayer() {
val sample = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
val sample2 = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/ElephantsDream.mp4"
if (player == null) {
player = SimpleExoPlayer.Builder(this).build()
mBinding.exoPlayerVVideoDetail.player = player
mBinding.exoPlayerVVideoDetail.controllerShowTimeoutMs = 0
}
/**
* 1개 영상 실행할 때
* */
val mediaSource: MediaSource = buildMediaSource(Uri.parse(sample))
player?.setMediaSource(mediaSource)
/**
* 재생목록 만들기 (다음 영상 추가)
* */
// 첫번째로 실행할 영상
val mediaItem = MediaItem.fromUri(sample)
player?.setMediaItem(mediaItem)
// 두번째로 실행할 영상
val secondMediaItem = MediaItem.fromUri(sample2)
player?.addMediaItem(secondMediaItem)
player?.playWhenReady = playWhenReady
player?.seekTo(currentWindow, playbackPosition)
player?.prepare()
}
fun buildMediaSource(uri: Uri): MediaSource {
val userAgent: String = Util.getUserAgent(this, "project_name")
return if (uri.lastPathSegment!!.contains("mp3") || uri.lastPathSegment!!.contains("mp4")) {
ProgressiveMediaSource.Factory(DefaultHttpDataSourceFactory(userAgent))
.createMediaSource(
MediaItem.fromUri(uri)
)
} else if (uri.lastPathSegment!!.contains("m3u8")) {
HlsMediaSource.Factory(DefaultHttpDataSourceFactory(userAgent)).createMediaSource(
MediaItem.fromUri(uri)
)
} else {
ProgressiveMediaSource.Factory(DefaultDataSourceFactory(this, userAgent))
.createMediaSource(
MediaItem.fromUri(uri)
)
}
}
fun releasePlayer() {
player?.let {
playWhenReady = it.playWhenReady
playbackPosition = it.currentPosition
currentWindow = it.currentWindowIndex
it.release()
player = null
}
}
@SuppressLint("InlinedApi")
private fun hideSystemUi() {
mBinding.exoPlayerVVideoDetail.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LOW_PROFILE
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
or View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
)
}
override fun onStart() {
super.onStart()
if (Util.SDK_INT >= 24) {
initializePlayer()
}
}
override fun onResume() {
super.onResume()
// hideSystemUi() -> 전체화면 환경으로 만들기
hideSystemUi()
if ((Util.SDK_INT < 24 || player == null)) {
initializePlayer()
}
}
override fun onPause() {
super.onPause()
if (Util.SDK_INT < 24) {
releasePlayer()
}
}
override fun onStop() {
super.onStop()
if (Util.SDK_INT >= 24) {
releasePlayer()
}
}
}
[참고]
출처 -
Media streaming with ExoPlayer
developer.android.com/codelabs/exoplayer-intro#0
무작정 앱만들기-6 (ExoPlayer로 간단한 뮤직 플레이어를 만들어보자)
안드로이드) ExoPlayer2 사용법
여러 개의 영상을 Exoplayer로 순차 재상하기
반응형
'IT > ANDROID' 카테고리의 다른 글
[ANDROID] ExoPlayer 영상 꽉차게 표시하기 (0) | 2021.01.19 |
---|---|
[ANDROID/KOTLIN] ExoPlayer로 YouTube 영상 실행하기 (2) | 2021.01.18 |
[ANDROID] 샘플 무료 동영상 경로 공유 (1) | 2021.01.12 |
[ANDROID/KOTLIN] 배경 색상 코드로 수정하기 (setBackgroundResource) (0) | 2021.01.12 |
[ANDROID/KOTLIN] layout_weight 코드로 수정하기 (0) | 2021.01.12 |