activity_main:
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">
<Button
android:id="@+id/fromAlbumBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="预览"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Play"/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Pause"/>
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Replay"/>
</LinearLayout>
<FrameLayout
android:id="@+id/framelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/stop">
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
/>
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</FrameLayout>
</LinearLayout>
MainActivity:
package com.example.playvideotest
import android.app.Activity
import android.content.ContentResolver
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.net.Uri
import android.os.Bundle
import android.provider.OpenableColumns
import android.view.View
import android.webkit.MimeTypeMap
import android.widget.Button
import android.widget.ImageView
import android.widget.VideoView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.net.toFile
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
val fromAlum=2
lateinit var uri:Uri
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val videoView=findViewById<VideoView>(R.id.videoView)
val play=findViewById<Button>(R.id.play)
val pause=findViewById<Button>(R.id.pause)
val replay=findViewById<Button>(R.id.replay)
val fromAlumBtn=findViewById<Button>(R.id.fromAlbumBtn)
val imageView=findViewById<ImageView>(R.id.imageView)
imageView.visibility=View.INVISIBLE
videoView.visibility=View.INVISIBLE
fromAlumBtn.setOnClickListener {
//打开文件选择器
val intent=Intent(Intent.ACTION_OPEN_DOCUMENT)
intent.addCategory(Intent.CATEGORY_OPENABLE)
intent.type="*/*"
startActivityForResult(intent,fromAlum)
}
play.setOnClickListener {
if (!videoView.isPlaying){
videoView.start()
}
}
pause.setOnClickListener {
if (videoView.isPlaying){
videoView.pause()
}
}
replay.setOnClickListener {
if (videoView.isPlaying){
videoView.resume()
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
when(requestCode){
fromAlum->{
if(resultCode== Activity.RESULT_OK && data!=null ){
data.data?.let{
uri=it
val filename=uriToFileName(uri,this)
val lastDot=filename.lastIndexOf(".")
val extension=filename.substring(lastDot+1).uppercase()
if (extension=="MP4"){
val videoView=findViewById<VideoView>(R.id.videoView)
val imageView = findViewById<ImageView>(R.id.imageView)
videoView.setVideoURI(uri)
imageView.visibility=View.INVISIBLE
videoView.visibility=View.VISIBLE
}else {
val bitmap = getBitmapFromUri(uri)
val imageView = findViewById<ImageView>(R.id.imageView)
val videoView=findViewById<VideoView>(R.id.videoView)
imageView.setImageBitmap(bitmap)
imageView.visibility=View.VISIBLE
videoView.visibility=View.INVISIBLE
}
}
}
}
}
}
private fun getBitmapFromUri(uri:Uri)=contentResolver.openFileDescriptor(uri,"r")?.use {
BitmapFactory.decodeFileDescriptor(it.fileDescriptor)
}
override fun onDestroy() {
super.onDestroy()
val videoView=findViewById<VideoView>(R.id.videoView)
videoView.suspend()
}
fun uriToFileName(uri: Uri, context: Context):String{
return when(uri.scheme){
ContentResolver.SCHEME_FILE->uri.toFile().name
ContentResolver.SCHEME_CONTENT->{
val cursor=context.contentResolver.query(uri,null,null,null,null,null)
cursor?.let {
it.moveToFirst()
val displayName=it.getString(it.getColumnIndex(OpenableColumns.DISPLAY_NAME))
cursor.close()
displayName
}?:"${System.currentTimeMillis()}.${MimeTypeMap.getSingleton().getExtensionFromMimeType(context.contentResolver.getType(uri))}}"
}
else -> "${System.currentTimeMillis()}.${MimeTypeMap.getSingleton().getExtensionFromMimeType(context.contentResolver.getType(uri))}}"
}
}
}
音频播放:
package com.example.playaudiotest
import android.media.MediaPlayer
import android.os.Bundle
import android.widget.Button
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
private val mediaPlayer=MediaPlayer()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initMediaPlayer()
val play=findViewById<Button>(R.id.play)
val pause=findViewById<Button>(R.id.pause)
val stop=findViewById<Button>(R.id.stop)
play.setOnClickListener {
if (!mediaPlayer.isPlaying){
mediaPlayer.start()
}
}
pause.setOnClickListener {
if (mediaPlayer.isPlaying){
mediaPlayer.pause()
}
}
stop.setOnClickListener {
if (mediaPlayer.isPlaying){
mediaPlayer.reset()
initMediaPlayer()
}
}
}
private fun initMediaPlayer(){
val assetManager=assets
val fd=assetManager.openFd("spoal.mp3")
mediaPlayer.setDataSource(fd.fileDescriptor,fd.startOffset,fd.length)
mediaPlayer.prepare()
}
override fun onDestroy() {
super.onDestroy()
mediaPlayer.stop()
mediaPlayer.release()
}
}