Android/Kotlin
[Android, Kotlin] VideoView
겨울시인
2022. 5. 6. 16:11
1. 기능 구현
- 가로 화면(Landscape) 으로 동영상을 Full Screen으로 재생해보자.
- 재생 동영상은 인터넷 URL로부터 받아 보도록 하자.
2. Android Studio에서 기본 프로젝트(with empty activity) 생성하자!
생성시 'Empty Activity'로 기본 생성
3. ViewBinding 사용을 위한 build.gradle 설정
android {
// 뷰 바인딩 옵션 활성화
viewBinding {
enabled = true
}
}
4. activity_main.xml
- VideoView 를 만들어 가로, 세로를 'match_parent' 를 통해 전체 화면 설정해두자.
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
5. AndroidManifest.xml (인터넷 권한, 가로 화면, No action bar)
- uses-permission android:name="android.permission.INTERNET" : 안드로이드 앱에서 인터넷 접속 권한을 허용
- android:usesCleartextTraffic="true" : 모든 http 사이트에 대한 접근을 허용
안드로이드9(APL Lv 28) 부터 강화된 네트워크 보안 정책으로 인하여 'HTTPS'가 모든 네트워크 트래픽의 기본값으로 설정됨에 따라 '암호화되지 않은 HTTP (Cleartext)'를 사용하기 위한 권한을 얻기 위해서는 해당 Flag 를 true로 해주어야만 오류가 발생하지 않는다.
- android:screenOrientation="landscape" : 앱 화면을 가로 모드로 설정
- android:theme="@style/Theme.AppCompat.NoActionBar" : 앱 실행시 상단에 보이는 ActionBar 영역 숨기기
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.videoview">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.VideoView"
android:usesCleartextTraffic="true">
<activity
android:name=".MainActivity"
android:screenOrientation="landscape"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
6. MainActivity.kt
- MediaController
- MediaPlayer 에 대한 컨트롤을 포함하는 View
- Ex)"Play/Pause", "Rewind", "Fast Forward" 버튼 등등
- MediaPlayer 의 상태와 동기화 처리를 담당한다.
- mediaController.setAnchorView(videoView)
- 전체 화면으로 보여지는 동영상(videoView)위에 mediaController 를 AnchorView 로 설정
- 즉 보여지는 화면 위에 떠있는 창으로 컨트롤 창을 띄워놓고 터치시 보여짐, 3초 유휴시간뒤 사라짐 설정
- Uri.parse : 재생할 동영상의 리소스 식별을 위해 URL을 가져와 파싱을 통해 URI 객체에 담아둠.
- 참고(1) URI (Uniform Resource Identifier) : 리소스를 식별하는 방식
- 참고(2) URL (Uniform Resource Locator) : 경로(위치)기반 리소스를 식별하는 방식
- setMediaController : videoView에 대한 컨트롤러를 설정
- setVideoURI : URI 객체에 담아둔 리소스를 재생할 동영상 리소스로 설정
- start() : 동영상 재생 시작
class MainActivity : AppCompatActivity() {
private var mBinding : ActivityMainBinding? = null
private val binding get() = mBinding!!
private val videoURL = "http://commondatastorage.googleapis.com/gtv-videos-bucket/sample/BigBuckBunny.mp4"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val videoView = binding.videoView
var mediaController = MediaController(this)
mediaController.setAnchorView(videoView)
var uri = Uri.parse(videoURL)
videoView.setMediaController(mediaController)
videoView.setVideoURI(uri)
videoView.start()
}
}
7. 실행결과
8. Reference
- 주의할점
참고한 동영상 강의는 Java 로 진행된 강의입니다. Kotlin 을 공부하기 위한 목적으로 Java-Kotlin 간의 다른 부분에 대해서는
수정하여 예제 작성을 하였고 영상이 올라온 시점이 3년 전이라 현재의 버전, API 들이 deprecated 된 경우도 있어
그러할 경우 현재 권장하는 API로 변경/적용 한 부분이 있어 Code가 다를 수 있으니 같이 보시는 분들은 참고 바람.