zoukankan      html  css  js  c++  java
  • 使用VideoView播放、暂停、快进视频

     1 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
     2     xmlns:tools="http://schemas.android.com/tools"
     3     android:layout_width="match_parent"
     4     android:layout_height="match_parent"
     5     tools:context="com.example.videoview.MainActivity" >
     6 
     7     <VideoView
     8         android:id="@+id/videoView"
     9         android:layout_width="match_parent"
    10         android:layout_height="416dp" />
    11 
    12     <LinearLayout
    13         android:id="@+id/linearLayout"
    14         android:layout_width="match_parent"
    15         android:layout_height="wrap_content"
    16         android:layout_gravity="bottom"
    17         android:orientation="horizontal" >
    18 
    19         <Button
    20             android:id="@+id/start"
    21             android:layout_width="wrap_content"
    22             android:layout_height="wrap_content"
    23             android:text="开始" />
    24 
    25         <Button
    26             android:id="@+id/pause"
    27             android:layout_width="wrap_content"
    28             android:layout_height="wrap_content"
    29             android:text="暂停" />
    30 
    31         <Button
    32             android:id="@+id/seekto"
    33             android:layout_width="wrap_content"
    34             android:layout_height="wrap_content"
    35             android:text="3秒处" />
    36     </LinearLayout>
    37 
    38 </FrameLayout>
    activity_main.xml

    测试主代码:

     1 package com.example.videotest;
     2 
     3 import java.io.File;
     4 
     5 import android.app.Activity;
     6 import android.os.Bundle;
     7 import android.os.Environment;
     8 import android.provider.MediaStore.Video;
     9 import android.view.Menu;
    10 import android.view.MenuItem;
    11 import android.view.View;
    12 import android.view.View.OnClickListener;
    13 import android.widget.Button;
    14 import android.widget.VideoView;
    15 
    16 public class MainActivity extends Activity implements OnClickListener {
    17 
    18     private VideoView videoView;
    19 
    20     @Override
    21     protected void onCreate(Bundle savedInstanceState) {
    22         super.onCreate(savedInstanceState);
    23         setContentView(R.layout.activity_main);
    24 
    25         videoView = (VideoView) findViewById(R.id.videoView);
    26 
    27         File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    28 
    29         File f = new File(path, "/Camera/test.mp4");
    30 
    31         videoView.setVideoPath(f.getAbsolutePath());
    32 
    33         Button start = (Button) findViewById(R.id.start);
    34         start.setOnClickListener(this);
    35         Button pause = (Button) findViewById(R.id.pause);
    36         pause.setOnClickListener(this);
    37         Button seekto = (Button) findViewById(R.id.seekto);
    38         seekto.setOnClickListener(this);
    39 
    40     }
    41 
    42     @Override
    43     public void onClick(View v) {
    44         switch (v.getId()) {
    45         case R.id.start:
    46             videoView.start();
    47             break;
    48         case R.id.pause:
    49             videoView.pause();
    50             break;
    51         case R.id.seekto:
    52             videoView.seekTo(3*1000);
    53             break;
    54         }
    55     }
    56 
    57 }
  • 相关阅读:
    文件名中含有空格读取时产生的异常
    R 常用清洗函数汇总
    Fluid 0.4 新版本正式发布:支持数据预热,优化小文件场景
    阿里云 Serverless 再升级,从体验上拉开差距
    Dubbo-go 源码笔记(二)客户端调用过程
    高质量的缺陷分析:让自己少写 bug
    微服务框架 Go-Micro 集成 Nacos 实战之服务注册与发现
    OpenYurt 深度解读:如何构建 Kubernetes 原生云边高效协同网络?
    在大规模 Kubernetes 集群上实现高 SLO 的方法
    双十一购物节,Nacos 1.4.0 + Go SDK 1.0.1发布
  • 原文地址:https://www.cnblogs.com/zzw1994/p/4997509.html
Copyright © 2011-2022 走看看