欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡單的視頻播放示例

 更新時間:2023年02月05日 10:25:26   作者:ObliviateOnline  
這篇文章主要為大家介紹了Android開發(fā)基礎(chǔ)實(shí)現(xiàn)最簡單的視頻播放示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jì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)文章

最新評論