{"id":1821,"date":"2023-01-16T16:06:26","date_gmt":"2023-01-16T08:06:26","guid":{"rendered":"https:\/\/badgameshow.com\/fly\/?p=1821"},"modified":"2023-01-16T16:06:26","modified_gmt":"2023-01-16T08:06:26","slug":"%e3%80%90android%e3%80%91videoview-%e6%92%ad%e6%94%be%e5%bd%b1%e7%89%87-%e7%af%84%e4%be%8b","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/fly\/%e3%80%90android%e3%80%91videoview-%e6%92%ad%e6%94%be%e5%bd%b1%e7%89%87-%e7%af%84%e4%be%8b\/","title":{"rendered":"\u3010Android\u3011VideoView \u64ad\u653e\u5f71\u7247 \u7bc4\u4f8b"},"content":{"rendered":"<h1>\u3010Android\u3011VideoView \u64ad\u653e\u5f71\u7247 \u7bc4\u4f8b<\/h1>\n<h4>VideoView \u662f Android \u6846\u67b6\u4e2d\u4e00\u500b\u7528\u65bc\u64ad\u653e\u8996\u983b\u7684\u985e\u3002\u5b83\u53ef\u4ee5\u7528\u65bc\u5728 Android \u61c9\u7528\u7a0b\u5e8f\u4e2d\u64ad\u653e\u672c\u5730\u6216\u9060\u7a0b\u8996\u983b\u3002\u5b83\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217 API\uff0c\u53ef\u4ee5\u7528\u65bc\u63a7\u5236\u8996\u983b\u7684\u64ad\u653e\u3001\u66ab\u505c\u3001\u505c\u6b62\u7b49\u3002<\/h4>\n<hr \/>\n<h4>\u6587\u7ae0\u76ee\u9304<\/h4>\n<ol>\n<li><a href=\"#a\">VideoView \u8b80\u53d6\u5f71\u7247<\/a><\/li>\n<li><a href=\"#b\">VideoView \u958b\u59cb\u3001\u66ab\u505c\u3001\u91cd\u64ad<\/a><\/li>\n<li><a href=\"#c\">VideoView \u62d6\u66f3\u5feb\u8f49<\/a><\/li>\n<li><a href=\"#d\">VideoView \u76e3\u807d\u64ad\u653e\u9032\u5ea6<\/a><\/li>\n<\/ol>\n<hr \/>\n<p><a id=\"a\"><\/a><\/p>\n<h4>1.VideoView \u8b80\u53d6\u5f71\u7247<\/h4>\n<h5>raw \u8cc7\u6599\u593e<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">binding.videoView.setVideoURI(Uri.parse(\"android.resource:\/\/<span class=\"katex math inline\">packageName\/<\/span>{R.raw.a}\"))\n<\/code><\/pre>\n<h5>\u7db2\u8def\u52a0\u8f09<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">binding.videoView.setVideoPath(\"https:\/\/firebasestorage.googleapis.com\/v0\/b\/jetpackdemo-b4bda.appspot.com\/o\/a.mp4?alt=media&amp;token=763afad8-d421-437e-b96c-064f4d95ab2b\")\n<\/code><\/pre>\n<p><a id=\"b\"><\/a><\/p>\n<h4>2.VideoView \u958b\u59cb\u3001\u66ab\u505c\u3001\u91cd\u64ad<\/h4>\n<h5>activity_main.xml<\/h5>\n<pre data-language=XML><code class=\"language-markup line-numbers\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;&lt;!-- Use DrawerLayout as root container for activity --&gt;\n&lt;layout xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\n    xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    xmlns:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"&gt;\n\n    &lt;data&gt;\n\n    &lt;\/data&gt;\n\n    &lt;androidx.constraintlayout.widget.ConstraintLayout\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\"&gt;\n\n        &lt;VideoView\n            android:id=\"@+id\/videoView\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"wrap_content\"\n            android:keepScreenOn=\"true\"\n            app:layout_constraintEnd_toEndOf=\"parent\"\n            app:layout_constraintStart_toStartOf=\"parent\"\n            app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n\n        &lt;androidx.appcompat.widget.AppCompatTextView\n            android:id=\"@+id\/time\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:textColor=\"@color\/black\"\n            android:textSize=\"20sp\"\n            android:layout_marginTop=\"10dp\"\n            app:layout_constraintEnd_toEndOf=\"parent\"\n            app:layout_constraintStart_toStartOf=\"parent\"\n            app:layout_constraintTop_toTopOf=\"parent\" \/&gt;\n\n        &lt;Button\n            android:layout_marginTop=\"16dp\"\n            android:id=\"@+id\/start\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:text=\"\u958b\u59cb\"\n            app:layout_constraintEnd_toStartOf=\"@+id\/pause\"\n            app:layout_constraintHorizontal_bias=\"0.5\"\n            app:layout_constraintStart_toStartOf=\"parent\"\n            app:layout_constraintTop_toBottomOf=\"@+id\/videoView\" \/&gt;\n\n        &lt;Button\n            android:id=\"@+id\/pause\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:text=\"\u66ab\u505c\"\n            app:layout_constraintEnd_toStartOf=\"@+id\/resume\"\n            app:layout_constraintHorizontal_bias=\"0.5\"\n            app:layout_constraintStart_toEndOf=\"@+id\/start\"\n            app:layout_constraintTop_toTopOf=\"@+id\/start\" \/&gt;\n\n        &lt;Button\n            android:id=\"@+id\/resume\"\n            android:layout_width=\"wrap_content\"\n            android:layout_height=\"wrap_content\"\n            android:text=\"\u91cd\u64ad\"\n            app:layout_constraintEnd_toEndOf=\"parent\"\n            app:layout_constraintHorizontal_bias=\"0.5\"\n            app:layout_constraintStart_toEndOf=\"@+id\/pause\"\n            app:layout_constraintTop_toTopOf=\"@+id\/pause\" \/&gt;\n\n    &lt;\/androidx.constraintlayout.widget.ConstraintLayout&gt;\n\n&lt;\/layout&gt;\n\n<\/code><\/pre>\n<h5>MainActivity.kt<\/h5>\n<pre><code class=\"language-kotlin line-numbers\">class MainActivity : AppCompatActivity() {\n\n    private lateinit var binding: ActivityMainBinding\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n\n        binding = ActivityMainBinding.inflate(layoutInflater)\n        setContentView(binding.root)\n\n        binding.start.setOnClickListener {\n            binding.videoView.start()\n        }\n\n        binding.pause.setOnClickListener {\n            binding.videoView.pause()\n        }\n\n        binding.resume.setOnClickListener {\n            binding.videoView.resume()\n        }\n    }\n\n}\n<\/code><\/pre>\n<p><a id=\"c\"><\/a><\/p>\n<h4>3.VideoView \u62d6\u66f3\u5feb\u8f49<\/h4>\n<pre><code class=\"language-kotlin line-numbers\">binding.videoView.setOnTouchListener { _, event -&gt;\n    when (event.action) {\n        MotionEvent.ACTION_MOVE -&gt; {\n            val position = binding.videoView.duration * event.x \/ binding.videoView.width\n            binding.videoView.seekTo(position.toInt())\n            binding.time.text = \"${(position \/ binding.videoView.duration * 100).roundToInt()}%\"\n        }\n    }\n    true\n}\n<\/code><\/pre>\n<p><a id=\"d\"><\/a><\/p>\n<h4>4.VideoView \u76e3\u807d\u64ad\u653e\u9032\u5ea6<\/h4>\n<pre><code class=\"language-kotlin line-numbers\">class MainActivity : AppCompatActivity() {\n\n    private lateinit var binding: ActivityMainBinding\n    private val choreographer = Choreographer.getInstance()\n\n    override fun onCreate(savedInstanceState: Bundle?) {\n        super.onCreate(savedInstanceState)\n\n        binding = ActivityMainBinding.inflate(layoutInflater)\n        setContentView(binding.root)\n\n        binding.videoView.setOnPreparedListener {\n            it.isLooping = true\n            it.start()\n\n            choreographer.postFrameCallback(object : Choreographer.FrameCallback {\n                override fun doFrame(frameTimeNanos: Long) {\n                    updateProgress()\n                    choreographer.postFrameCallback(this)\n                }\n            })\n        }\n    }\n\n    private fun updateProgress() {\n        val position = binding.videoView.currentPosition * 100\n        binding.time.text = \"${(position \/ binding.videoView.duration)}%\"\n    }\n\n}\n<\/code><\/pre>\n\n<div style=\"font-size: 0px; height: 0px; line-height: 0px; margin: 0; padding: 0; clear: both;\"><\/div>","protected":false},"excerpt":{"rendered":"<p>\u3010Android\u3011VideoView \u64ad\u653e\u5f71\u7247 \u7bc4\u4f8b VideoView \u662f Android \u6846\u67b6\u4e2d\u4e00\u500b\u7528\u65bc\u64ad &hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"pgc_sgb_lightbox_settings":"","footnotes":""},"categories":[5],"tags":[13,15,232],"class_list":["post-1821","post","type-post","status-publish","format-standard","hentry","category-android","tag-android","tag-kotlin","tag-videoview"],"_links":{"self":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/comments?post=1821"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1821\/revisions"}],"predecessor-version":[{"id":1822,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/posts\/1821\/revisions\/1822"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/media?parent=1821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/categories?post=1821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/fly\/wp-json\/wp\/v2\/tags?post=1821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}