Android VideoView播放视频控制:开始、暂停、快进(3)
本文在附录参考文章(1)的基础上增加的Android VideoView播放视频时候的控制。控制主要依赖VideoView的start(开始),pause(暂停),seekTo(快进,跳到某一个时间点开始)
先写一个布局文件(增加控制按钮):

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context="com.example.videoview.MainActivity" >    <VideoView        android:id="@+id/videoView"        android:layout_width="match_parent"        android:layout_height="match_parent" />    <LinearLayout        android:id="@+id/linearLayout"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:layout_gravity="bottom"        android:orientation="horizontal" >        <Button            android:id="@+id/start"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="开始" />        <Button            android:id="@+id/pause"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="暂停" />        <Button            android:id="@+id/seekto"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="3秒处" />    </LinearLayout></FrameLayout>


Java代码:

package com.example.videoview;import java.io.File;import android.app.Activity;import android.os.Bundle;import android.os.Environment;import android.view.View;import android.widget.Button;import android.widget.VideoView;public class MainActivity extends Activity implements Button.OnClickListener {private VideoView videoView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);videoView = (VideoView) findViewById(R.id.videoView);// 获得的path等于:/storage/emulated/0/DCIMFile path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);// 拼接完整路径File f = new File(path, "/Camera/test.mp4");// 此时的f.getAbsolutePath()=/storage/emulated/0/DCIM//\Camera/test.mp4videoView.setVideoPath(f.getAbsolutePath());// 开始播放视频// videoView.start();// VideiView获焦点// videoView.requestFocus();Button start = (Button) findViewById(R.id.start);start.setOnClickListener(this);Button pause = (Button) findViewById(R.id.pause);pause.setOnClickListener(this);Button seekto = (Button) findViewById(R.id.seekto);seekto.setOnClickListener(this);}@Overridepublic void onClick(View v) {switch (v.getId()) {// 开始播放case R.id.start:videoView.start();break;// 暂停case R.id.pause:videoView.pause();break;// 3秒处case R.id.seekto:videoView.seekTo(3 * 1000);break;default:break;}}}



附录参考文章:
【1】《Android VideoView播放视频(1)》链接地址:http://blog.csdn.net/zhangphil/article/details/49885697
【2】《Android VideoView播放在线视频(2)》链接地址:http://blog.csdn.net/zhangphil/article/details/49885995

更多相关文章

  1. android 细节之android.view.InflateException: Binary XML file
  2. android渐隐动画,通过xml控制按钮的变化。
  3. Android实现文字滚动播放效果
  4. Android开发系列(二十二):AdapterViewFlipper的功能和使用方法
  5. Android(安卓)播放视频常见问题小结
  6. activity介绍和值的传递(寒假学习2)
  7. 控制drawable资源大小
  8. Android(安卓)Studio V3.12环境下TV开发教程(八)使用Leanback运输
  9. Android前台服务 音乐播放器 前台服务

随机推荐

  1. android截屏代码实现方法
  2. 只会写xml和Activity,做android真的不行!
  3. 10个常见的 Android(安卓)新手误区
  4. Android之Android的数据存储--SD卡
  5. Android(安卓)sdk manager不能更新下载缓
  6. 【转官方】Android(安卓)ADB调试命令、支
  7. webview中的javascript调用android命令实
  8. 五年之内,Android(安卓)或将被取代?
  9. android IBeacon 开发(一)搜索IBeacon基站
  10. 《转》给Android应用开发者的十个建议