79 lines
2.6 KiB
Kotlin
79 lines
2.6 KiB
Kotlin
package com.example.swiper
|
|
|
|
import android.Manifest
|
|
import android.app.Activity
|
|
import android.content.Intent
|
|
import android.content.pm.PackageManager
|
|
import android.os.Build
|
|
import android.os.Bundle
|
|
import android.os.Environment
|
|
import android.widget.Button
|
|
import android.widget.VideoView
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
|
import androidx.annotation.RequiresApi
|
|
import androidx.appcompat.app.AppCompatActivity
|
|
import androidx.core.content.ContextCompat
|
|
import androidx.core.net.toUri
|
|
import java.io.File
|
|
|
|
class MainActivity : AppCompatActivity() {
|
|
private var files: List<File>? = null
|
|
private var selected = 0
|
|
|
|
private var resultLauncher =
|
|
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
|
if (result.resultCode == Activity.RESULT_OK) {
|
|
val data: Intent? = result.data
|
|
val paths = data?.data?.path?.split(":") ?: return@registerForActivityResult
|
|
val path = Environment.getExternalStorageDirectory().path + '/' + paths[1]
|
|
val directory = File(path)
|
|
files = directory.listFiles()?.filter { file -> file.name.endsWith(".webm") }
|
|
val video = findViewById<VideoView>(R.id.videoView)
|
|
video.setVideoURI(files!![0].toUri())
|
|
video.start()
|
|
}
|
|
}
|
|
|
|
@RequiresApi(Build.VERSION_CODES.M)
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
setContentView(R.layout.activity_main)
|
|
|
|
if (ContextCompat.checkSelfPermission(
|
|
this,
|
|
Manifest.permission.READ_EXTERNAL_STORAGE
|
|
) == PackageManager.PERMISSION_DENIED
|
|
) {
|
|
requestPermissions(arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), 0x3)
|
|
}
|
|
|
|
|
|
findViewById<Button>(R.id.pick).setOnClickListener {
|
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
|
resultLauncher.launch(intent)
|
|
}
|
|
|
|
val video = findViewById<VideoView>(R.id.videoView)
|
|
|
|
video.setOnPreparedListener {
|
|
it.isLooping = true
|
|
}
|
|
|
|
video.setOnClickListener {
|
|
if (video.isPlaying) video.pause()
|
|
else video.start()
|
|
}
|
|
|
|
findViewById<Button>(R.id.next).setOnClickListener {
|
|
selected++
|
|
video.setVideoURI(files!![selected].toUri())
|
|
video.start()
|
|
}
|
|
|
|
findViewById<Button>(R.id.prev).setOnClickListener {
|
|
selected--
|
|
video.setVideoURI(files!![selected].toUri())
|
|
video.start()
|
|
}
|
|
}
|
|
} |