videoView만들기

노지수·2022년 2월 7일
0

비디오파일 저장

app - src - main - res - raw폴더에 동영상 넣어두기

activity_main

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    <VideoView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/videoView"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

class MainActivity : AppCompatActivity() {

    val b by lazy { ActivityMainBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(b.root)
        // MainActivity에 videoView를 실행하기위한 코드 작성
        b.videoView.setMediaController(MediaController(this))

		// 실행할 비디오파일 경로
        b.videoView.setVideoURI(
            Uri.parse("android.resource://" + packageName + "/" + R.raw.music)
        )
    }
}

결과

앱 실행시 첫 화면


정상적으로 동영상 재생

profile
프로그래밍, 개념 및 이론 기록

0개의 댓글