1、播放音频
< LinearLayout xmlns: android= "http://schemas.android.com/apk/res/android"
android: orientation= "vertical"
android: layout_width= "match_parent"
android: layout_height= "match_parent" >
< Button
android: id= "@+id/play"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: text= "Play" / >
< Button
android: id= "@+id/pause"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: text= "Pause" / >
< Button
android: id= "@+id/stop"
android: layout_width= "match_parent"
android: layout_height= "wrap_content"
android: text= "Stop" / >
< / LinearLayout>
package com. jpc. playaudiotest
import android. media. MediaPlayer
import androidx. appcompat. app. AppCompatActivity
import android. os. Bundle
import android. widget. Button
class MainActivity : AppCompatActivity ( ) {
private val mediaPlayer = MediaPlayer ( )
override fun onCreate ( savedInstanceState: Bundle? ) {
super . onCreate ( savedInstanceState)
setContentView ( R. layout. activity_main)
initMediaPlayer ( )
val btnPlay = findViewById< Button> ( R. id. play)
btnPlay. setOnClickListener {
if ( ! mediaPlayer. isPlaying) {
mediaPlayer. start ( )
}
}
val btnPause = findViewById< Button> ( R. id. pause)
btnPause. setOnClickListener {
if ( mediaPlayer. isPlaying) {
mediaPlayer. pause ( )
}
}
val btnStop = findViewById< Button> ( R. id. stop)
btnStop. setOnClickListener {
if ( mediaPlayer. isPlaying) {
mediaPlayer. reset ( )
initMediaPlayer ( )
}
}
}
private fun initMediaPlayer ( ) {
val assetManager = assets
val openFd = assetManager. openFd ( "凉凉-张碧晨-144597586.mp3" )
mediaPlayer. setDataSource ( openFd. fileDescriptor, openFd. startOffset, openFd. length)
mediaPlayer. prepare ( )
}
override fun onDestroy ( ) {
super . onDestroy ( )
mediaPlayer. stop ( )
mediaPlayer. release ( )
}
}
2、播放视频
< LinearLayout xmlns: android= " http://schemas.android.com/apk/res/android"
android: orientation= " vertical"
android: layout_width= " match_parent"
android: layout_height= " match_parent" >
< LinearLayout
android: layout_width= " match_parent"
android: layout_height= " wrap_content" >
< Button
android: id= " @+id/play_video"
android: layout_width= " 0dp"
android: layout_height= " wrap_content"
android: layout_weight= " 1"
android: text= " Play" />
< Button
android: id= " @+id/pause_video"
android: layout_width= " 0dp"
android: layout_height= " wrap_content"
android: layout_weight= " 1"
android: text= " Pause" />
< Button
android: id= " @+id/replay_video"
android: layout_width= " 0dp"
android: layout_height= " wrap_content"
android: layout_weight= " 1"
android: text= " Replay" />
</ LinearLayout>
< VideoView
android: id= " @+id/videoView"
android: layout_width= " match_parent"
android: layout_height= " wrap_content" />
</ LinearLayout>
package com. jpc. playaudiotest
import android. net. Uri
import androidx. appcompat. app. AppCompatActivity
import android. os. Bundle
import android. widget. Button
import android. widget. VideoView
class PlayVideoActivity : AppCompatActivity ( ) {
private lateinit var videoView: VideoView
override fun onCreate ( savedInstanceState: Bundle? ) {
super . onCreate ( savedInstanceState)
setContentView ( R. layout. activity_play_video)
val uri = Uri. parse ( "android.resource:// $ packageName / ${ R. raw. video } " )
videoView = findViewById< VideoView> ( R. id. videoView)
videoView. setVideoURI ( uri)
val btnPlay = findViewById< Button> ( R. id. play_video)
btnPlay. setOnClickListener {
if ( ! videoView. isPlaying) {
videoView. start ( )
}
}
val btnPause = findViewById< Button> ( R. id. pause_video)
btnPause. setOnClickListener {
if ( videoView. isPlaying) {
videoView. pause ( )
}
}
val btnReplay = findViewById< Button> ( R. id. replay_video)
btnReplay. setOnClickListener {
if ( videoView. isPlaying) {
videoView. resume ( )
}
}
}
override fun onDestroy ( ) {
super . onDestroy ( )
videoView. suspend ( )
}
}