타닥타닥 개발자의 일상

Kotlin 안드로이드 화면에 videoView이용해 동영상 삽입하기 본문

코딩 기록/Kotlin

Kotlin 안드로이드 화면에 videoView이용해 동영상 삽입하기

NomadHaven 2022. 2. 7. 17:37
파일 및 폴더 구성

raw 폴더 생성하고 music.mp4 동영상(혹은 자신이 원하는 동영상) 삽입.

 

activity_main.xml
<?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.kt

 

package com.example.sample21

import android.net.Uri
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.MediaController
import android.widget.VideoView

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val videoView = findViewById<VideoView>(R.id.videoView)

        videoView.setMediaController(MediaController(this))
        //videoView에 MediaController연결

        videoView.setVideoURI(Uri.parse("android.resource://" + packageName + "/"+R.raw.music))
   		//resource 폴더 안 raw 폴더 안에 있는 music이라는 동영상 연결
   }
}
결과화면

Comments