Android VideoView播放视频时候的控制。控制主要依赖VideoView的start(开始),pause(暂停),seekTo(快进,跳到某一个时间点开始)
一、Layout布局文件
<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="2秒处" />
</LinearLayout>
</FrameLayout>
二、Java代码
package com.example.OnVideo;
import java.io.File;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.VideoView;
public class MainActivity extends Activity implements Button.OnClickListener {
private VideoView video;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
video = (VideoView) findViewById(R.id.videoView);
// 获得的path等于:/storage/emulated/0/DCIM
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
// 拼接完整路径
File f = new File(path, "/Camera/test.mp4");
// 此时的f.getAbsolutePath()=/storage/emulated/0/DCIM//\Camera/test.mp4
if (null != f) {
video.setVideoPath(f.getAbsolutePath());
} else {
Log.d("=====", "路径为空");
}
// 开始播放视频
// 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);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.start: {
// 开始播放
video.start();
break;
}
case R.id.pause: {
// 暂停
video.pause();
break;
}
case R.id.seekto: {
// 3秒处
video.seekTo(2000);
break;
}
default:
break;
}
}
}
三、添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>