• 视频播放 (一) VideoView的使用


    1. 配置参数

      1.1 AndroidManifest.xml 文件添加网络权限

    <uses-permission android:name="android.permission.INTERNET" />

      1.2 http 明文请求设置

      android:usesCleartextTraffic="true"

      1.3 activity 配置屏幕变化,不重新加载 Activity

    1. //屏幕方向发生变更 - 用户旋转设备/屏幕布局发生变更/当前屏幕尺寸发生变更
    2. <activity
    3. android:name=".MainActivity"
    4. android:configChanges="orientation|screenLayout|screenSize"
    5. android:exported="true">

      1.4 开启协程引用库

    implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.6.0-alpha03'

      1.5.视频渲染 Surface View 或者 Texture View

    2. 布局文件 activity_main.xml

    1. "1.0" encoding="utf-8"?>
    2. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    3. xmlns:app="http://schemas.android.com/apk/res-auto"
    4. xmlns:tools="http://schemas.android.com/tools"
    5. android:layout_width="match_parent"
    6. android:layout_height="match_parent"
    7. tools:context=".MainActivity">
    8. <VideoView
    9. android:id="@+id/videoView"
    10. android:layout_width="2000dp"
    11. android:layout_height="0dp"
    12. app:layout_constraintBottom_toBottomOf="parent"
    13. app:layout_constraintEnd_toEndOf="parent"
    14. app:layout_constraintStart_toStartOf="parent"
    15. app:layout_constraintTop_toTopOf="parent" />
    16. <ProgressBar
    17. android:id="@+id/progressBar"
    18. style="?android:attr/progressBarStyleHorizontal"
    19. android:layout_width="0dp"
    20. android:layout_height="wrap_content"
    21. app:layout_constraintBottom_toBottomOf="parent"
    22. app:layout_constraintEnd_toEndOf="parent"
    23. app:layout_constraintStart_toStartOf="parent" />
    24. <ProgressBar
    25. android:id="@+id/progressBar2"
    26. style="?android:attr/progressBarStyle"
    27. android:layout_width="wrap_content"
    28. android:layout_height="wrap_content"
    29. app:layout_constraintBottom_toBottomOf="parent"
    30. app:layout_constraintEnd_toEndOf="parent"
    31. app:layout_constraintStart_toStartOf="parent"
    32. app:layout_constraintTop_toTopOf="parent" />
    33. androidx.constraintlayout.widget.ConstraintLayout>

    3. 使用 VideoView, MainActivity.kt

    1. class MainActivity : AppCompatActivity() {
    2. private lateinit var videoView: VideoView
    3. private var savePosition: Int = 0
    4. override fun onCreate(savedInstanceState: Bundle?) {
    5. super.onCreate(savedInstanceState)
    6. setContentView(R.layout.activity_main)
    7. // 26.51
    8. val progressBar = findViewById(R.id.progressBar)
    9. val progressBar2 = findViewById(R.id.progressBar2)
    10. //val videoPath = "android.resource://$packageName/${R.raw.red}"
    11. val videoPath = "https://media.w3.org/2010/05/sintel/trailer.mp4"
    12. videoView = findViewById(R.id.videoView)
    13. videoView.setVideoPath(videoPath)
    14. //设置导航控制布局
    15. videoView.setMediaController(MediaController(this))
    16. videoView.setOnPreparedListener {
    17. //Log.i("MyTag", "$it")
    18. //it.seekTo(3000)
    19. //duration 视频总的长度
    20. progressBar2.visibility = INVISIBLE
    21. progressBar.max = it.duration
    22. it.isLooping = true//循环播放
    23. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    24. it.playbackParams = PlaybackParams().apply {
    25. speed = 1f //倍数播放
    26. // pitch = 0.7f //变声,提高音调 尖锐,减少低沉
    27. }
    28. }
    29. it.seekTo(savePosition)
    30. //it.start()
    31. }
    32. lifecycleScope.launch {
    33. while (true) {
    34. if (videoView.isPlaying) {
    35. progressBar.progress = videoView.currentPosition
    36. }
    37. delay(500)
    38. }
    39. }
    40. //videoView.start()
    41. }
    42. override fun onPause() {
    43. super.onPause()
    44. savePosition = videoView.currentPosition
    45. }
    46. }

    4. 效果图

  • 相关阅读:
    Centos7使用nginx搭建rtmp流媒体服务器
    a single dex file (# methods: 67938 > 65536)
    Springboot毕设项目基于springboot的疫情物资运输管理系统4rs1u(java+VUE+Mybatis+Maven+Mysql)
    lv8 嵌入式开发-网络编程开发 19 原始套接字
    Go应用程序的安全最佳实践
    如何在远程协同视频会议中确保安全性?
    Zotero(3)---使用茉莉花插件提取中文文献信息
    神经网络需要的数学知识,神经网络的数学基础
    三次握手与四次挥的问题,怎么回答?
    STM32--ESP8266物联网WIFI模块(贝壳物联)--远程无线控制点灯
  • 原文地址:https://blog.csdn.net/u011193452/article/details/128135922