zoukankan      html  css  js  c++  java
  • 【Android】20.2 视频播放

    分类:C#、Android、VS2015;

    创建日期:2016-03-11

    一、简介

    本节例子和上一节的音频播放例子相似,也是最简单的示例,比如并没有考虑视频播放过程中电话打入的情况,也没有考虑复杂的控制。总之,如果你希望进一步学习复杂的例子,首先要先把最核心的简单例子搞明白,否则你连基本的设计思路都不知道,直接看复杂的例子或者实际项目中的代码肯定“事倍功半”,主要原因是你做不到举一反三,只会照搬,稍微让你修改一下功能你就晕了。

    实现视频播放的常见方式有:

    1、用VideoView和MediaController类实现视频播放

    常用,在布局文件中使用VideoView结合MediaController来实现对其控制。本节主要演示它的基本用法。

    VideoView控件可以从各种不同的来源(如文件系统、网站)下载视频文件并将视频播放出来。该控件提供了各种控制选项,例如控制视频界面的大小、缩放、着色等。

    如果需要更细粒度的控制,也可以用MediaPlayer类来实现。

    2、用SurfaceView和MediaPlayer类实现视频播放

    这是早期版本提供的办法,实现代码较多。对这种实现有兴趣的可参考其他资料。

    二、基本用法示例

    1、运行截图

    下面的截图是单击【播放RAW下的视频】按钮后看到的视频播放效果。

    image

    单击【停止播放】按钮后,可继续单击【播放SD卡Download下的视频】按钮观察,这里就不再截图了。

    2、设计步骤

    (1)添加视频文件

    由于只是为了演示,所以该例子Raw文件夹下和SD卡的Download文件夹下存放的是同一个视频文件(videoviewdemo.mp4)。

    至于如何将文件复制到SD卡上,请参看【常见问题解答】。

    (2)添加ch2002Main.axml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <Button
            android:id="@+id/ch2002_btnRawStart"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="播放RAW下的视频" />
      <Button
          android:id="@+id/ch2002_btnSdcardStart"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:text="播放SD卡Dwonload下的视频" />
      <Button
            android:id="@+id/ch2002_btnStop"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="停止播放" />
        <VideoView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/ch2002_videoView1"
            android:layout_margin="20dp" />
    </LinearLayout>

    (3)添加ch2002MainActivity.cs

    using Android.App;
    using Android.Content;
    using Android.OS;
    using Android.Widget;
    using Android.Net;
    
    namespace MyDemos.SrcDemos
    {
        [Activity(Label = "例20-2 视频播放基本用法")]
        [IntentFilter(new[] { Intent.ActionMain }, Categories = new[] { ch.MyDemosCategory })]
        public class ch2002MainActivity : Activity
        {
            VideoView mVideoView;
            Button btnRawStart, btnSdcardStart, btnStop;
    
            protected override void OnCreate(Bundle savedInstanceState)
            {
                base.OnCreate(savedInstanceState);
    
                SetContentView(Resource.Layout.ch2002Main);
                mVideoView = FindViewById<VideoView>(Resource.Id.ch2002_videoView1);
                btnRawStart = FindViewById<Button>(Resource.Id.ch2002_btnRawStart);
                btnSdcardStart = FindViewById<Button>(Resource.Id.ch2002_btnSdcardStart);
    
                Uri rawUri = Uri.Parse($"android.resource://{PackageName}/{Resource.Raw.videoviewdemo}");
                Uri sdcardUri = Uri.Parse($"{Environment.ExternalStorageDirectory.Name}/Download/videoviewdemo.mp4");
    
                btnRawStart.Click += delegate
                {
                    StartVideo(rawUri);
                };
    
                btnSdcardStart.Click += delegate
                {
                    StartVideo(sdcardUri);
                };
    
                btnStop = FindViewById<Button>(Resource.Id.ch2002_btnStop);
                btnStop.Click += delegate
                {
                    mVideoView.StopPlayback();
                    ChangePlayingState(true, false);
                };
            }
    
            private void StartVideo(Uri uri)
            {
                mVideoView.SetVideoURI(uri);
                mVideoView.SetMediaController(new MediaController(this));
                mVideoView.Start();
                ChangePlayingState(false, true);
            }
    
            private void ChangePlayingState(bool startEnabled,bool stopEnabled)
            {
                btnRawStart.Enabled = btnSdcardStart.Enabled = startEnabled;
                btnStop.Enabled = stopEnabled;
            }
        }
    }
  • 相关阅读:
    OpenResty 社区王院生:APISIX 的高性能实践
    【社群话题分享】有哪些程序员专属的“土味情话”?
    第一次亲密接触——二狗子初识 CDN
    又拍云叶靖:OpenResty 在又拍云存储中的应用
    【社群话题分享】程序员中年真这么难?
    Zabbix日常监控之lvs监控
    zabbix日常监控Apache2.4
    Centos7 apache2.4.29(httpd) 安装
    grafana的安装与设置(一)
    zabbix日常监控项java(四又分之一,通用性强)
  • 原文地址:https://www.cnblogs.com/rainmj/p/5265193.html
Copyright © 2011-2022 走看看