Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡單的視頻播放示例
正篇
視頻播放是很平常的一件事情,但如何在APP中實(shí)現(xiàn)呢,其實(shí)蠻簡單的,方法也很多,但作為基礎(chǔ)的就是使用VideoView了,下面我們來看看如何使用它。
使用方法
首先我們在項(xiàng)目中的res資源文件夾下新建一個新的文件夾“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>
我們在布局中把VideoView添加進(jìn)去,然后再加三個按鈕來控制視頻播放,用于重播,播放與暫停視頻。
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) { //開始播放 binding.videoView.start() } } binding.pause.setOnClickListener { if (binding.videoView.isPlaying) { //暫停播放 binding.videoView.pause() } } } }
寫完布局文件,我們再回到Activity文件中,把mp4文件通過Uri.parse()方法解析成Uri對象然后用VideoView的setVideoURI()方法傳入Uri對象即可完成初始化,然后我們通過它的start(),pause(),resume()以及suspend()方法實(shí)現(xiàn)視頻的播放,暫停,重播以及釋放資源。
最終效果展示
運(yùn)行后如下效果:
總結(jié)
這個控件限制蠻多的,很多格式視頻不支持,而且也是封裝后的,有時間可以再看看播放器相關(guān)的知識,下次再出一篇文章來詳細(xì)說說。
以上就是Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡單的視頻播放示例的詳細(xì)內(nèi)容,更多關(guān)于Android開發(fā)簡單視頻播放的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Android實(shí)現(xiàn)沉浸式狀態(tài)欄功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)沉浸式狀態(tài)欄功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10TextInputLayout輸入框控件的懸浮標(biāo)簽
這篇文章主要為大家詳細(xì)介紹了TextInputLayout輸入框控件的懸浮標(biāo)簽,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-12-12基于Android實(shí)現(xiàn)隨手指移動的ImageView
這篇文章主要介紹了基于Android實(shí)現(xiàn)隨手指移動的ImageView的相關(guān)資料,需要的朋友可以參考下2016-01-01Android RecyclerView區(qū)分視圖類型的Divider的實(shí)現(xiàn)
本篇文章主要介紹了Android RecyclerView區(qū)分視圖類型的Divider的實(shí)現(xiàn),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04Android開發(fā)中如何去掉app標(biāo)題欄的實(shí)現(xiàn)
這篇文章主要介紹了Android開發(fā)中如何去掉app標(biāo)題欄的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04Android 開發(fā)之Dialog中隱藏鍵盤的正確使用方法
這篇文章主要介紹了Android 開發(fā)之Dialog中隱藏鍵盤的正確使用方法的相關(guān)資料,主要說明Dialog 隱藏鍵盤的注意事項(xiàng),需要的朋友可以參考下2017-09-09Android中Intent組件的入門學(xué)習(xí)心得
Intent組件雖然不是四大組件,但卻是連接四大組件的橋梁,學(xué)習(xí)好這個知識,也非常的重要,下面這篇文章主要給大家介紹了關(guān)于Android中Intent組件的相關(guān)資料,需要的朋友可以參考下2021-12-12