Android開(kāi)發(fā)基礎(chǔ)實(shí)現(xiàn)最簡(jiǎn)單的視頻播放示例
正篇
視頻播放是很平常的一件事情,但如何在APP中實(shí)現(xiàn)呢,其實(shí)蠻簡(jiǎn)單的,方法也很多,但作為基礎(chǔ)的就是使用VideoView了,下面我們來(lái)看看如何使用它。

使用方法
首先我們?cè)陧?xiàng)目中的res資源文件夾下新建一個(gè)新的文件夾“raw”

然后我們把MP4文件放到該文件夾下即可

接著我們先把布局完成,以方便后續(xù)操作,布局文件代碼如下
XML布局代碼:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<VideoView
android:id="@+id/videoView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/replay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/str_replay"/>
<Button
android:id="@+id/play"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/str_play"/>
<Button
android:id="@+id/pause"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/str_pause"/>
</LinearLayout>
</LinearLayout>
我們?cè)诓季种邪裋ideoView添加進(jìn)去,然后再加三個(gè)按鈕來(lái)控制視頻播放,用于重播,播放與暫停視頻。
Activity文件代碼如下:
package com.example.myapplication
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.example.myapplication.databinding.ActivivtyPlayVideoBinding
class ActivityPlayVideo :AppCompatActivity() {
lateinit var binding : ActivivtyPlayVideoBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivivtyPlayVideoBinding.inflate(layoutInflater)
setContentView(binding.root)
val uri = Uri.parse("android.resource://$packageName/${R.raw.video}")
binding.videoView.setVideoURI(uri)
//處理播放控件
initVideo()
}
override fun onDestroy() {
super.onDestroy()
//釋放
binding.videoView.suspend()
}
private fun initVideo() {
binding.replay.setOnClickListener {
if (binding.videoView.isPlaying) {
//重新播放
binding.videoView.resume()
}
}
binding.play.setOnClickListener {
if (!binding.videoView.isPlaying) {
//開(kāi)始播放
binding.videoView.start()
}
}
binding.pause.setOnClickListener {
if (binding.videoView.isPlaying) {
//暫停播放
binding.videoView.pause()
}
}
}
}
寫完布局文件,我們?cè)倩氐紸ctivity文件中,把mp4文件通過(guò)Uri.parse()方法解析成Uri對(duì)象然后用VideoView的setVideoURI()方法傳入U(xiǎn)ri對(duì)象即可完成初始化,然后我們通過(guò)它的start(),pause(),resume()以及suspend()方法實(shí)現(xiàn)視頻的播放,暫停,重播以及釋放資源。
最終效果展示
運(yùn)行后如下效果:

總結(jié)
這個(gè)控件限制蠻多的,很多格式視頻不支持,而且也是封裝后的,有時(shí)間可以再看看播放器相關(guān)的知識(shí),下次再出一篇文章來(lái)詳細(xì)說(shuō)說(shuō)。
以上就是Android開(kāi)發(fā)基礎(chǔ)實(shí)現(xiàn)最簡(jiǎn)單的視頻播放示例的詳細(xì)內(nèi)容,更多關(guān)于Android開(kāi)發(fā)簡(jiǎn)單視頻播放的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android使用KeyStore對(duì)數(shù)據(jù)進(jìn)行加密的示例代碼
這篇文章主要介紹了Android使用KeyStore對(duì)數(shù)據(jù)進(jìn)行加密的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
Android入門之動(dòng)態(tài)BroadCast的使用教程
系統(tǒng)自己在很多時(shí)候都會(huì)發(fā)送廣播,比如電量低或者充足,剛啟動(dòng)完,插入耳機(jī),你有一條新的微信消息,這種都是使用BroadCast機(jī)制去實(shí)現(xiàn)的。BroadCast分為靜態(tài)和動(dòng)態(tài)BroadCast兩種,本文就來(lái)聊聊動(dòng)態(tài)BroadCast的使用,需要的可以參考一下2022-12-12
Android通知欄前臺(tái)服務(wù)的實(shí)現(xiàn)
這篇文章主要介紹了Android通知欄前臺(tái)服務(wù)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Android入門:多線程斷點(diǎn)下載詳細(xì)介紹
本篇文章主要介紹了 Android多線程斷點(diǎn)下載,即文件在下載一部分中斷后,可繼續(xù)接著已有進(jìn)度下載,有需要的可以了解一下。2016-11-11
Android?IntentFilter的匹配規(guī)則示例詳解
這篇文章主要為大家介紹了Android?IntentFilter的匹配規(guī)則示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android中Activity生命周期和啟動(dòng)模式詳解
這篇文章主要介紹了Activity生命周期和啟動(dòng)模式詳解的相關(guān)資料,需要的朋友可以參考下2016-07-07
Android中截取當(dāng)前屏幕圖片的實(shí)例代碼
該篇文章是說(shuō)明在Android手機(jī)或平板電腦中如何實(shí)現(xiàn)截取當(dāng)前屏幕的功能,并把截取的屏幕保存到SDCard中的某個(gè)目錄文件夾下面。實(shí)現(xiàn)的代碼如下:2013-08-08

