广告
返回顶部
首页 > 资讯 > 移动开发 >Android开发基础实现最简单的视频播放示例
  • 766
分享到

Android开发基础实现最简单的视频播放示例

Android开发简单视频播放Android视频播放 2023-02-05 15:02:38 766人浏览 独家记忆
摘要

目录正篇使用方法最终效果展示总结正篇 视频播放是很平常的一件事情,但如何在APP中实现呢,其实蛮简单的,方法也很多,但作为基础的就是使用VideoView了,下面我们来看看如何使

正篇

视频播放是很平常的一件事情,但如何在APP中实现呢,其实蛮简单的,方法也很多,但作为基础的就是使用VideoView了,下面我们来看看如何使用它。

使用方法

首先我们在项目中的res资源文件夹下新建一个新的文件夹“raw”

然后我们把MP4文件放到该文件夹下即可

接着我们先把布局完成,以方便后续操作,布局文件代码如下

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添加进去,然后再加三个按钮来控制视频播放,用于重播,播放与暂停视频。

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()方法实现视频的播放,暂停,重播以及释放资源。

最终效果展示

运行后如下效果:

总结

这个控件限制蛮多的,很多格式视频不支持,而且也是封装后的,有时间可以再看看播放器相关的知识,下次再出一篇文章来详细说说。

以上就是Android开发基础实现最简单的视频播放示例的详细内容,更多关于Android开发简单视频播放的资料请关注编程网其它相关文章!

--结束END--

本文标题: Android开发基础实现最简单的视频播放示例

本文链接: https://www.lsjlt.com/news/194230.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作