Videoview 实现:暂停 播放 快进 启动


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;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	
		videoView = (VideoView) findViewById(R.id.videoView);
	
		//获得的path等于:/storage/emulated/0/DICIM
		File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
		
		//拼接完整路径
		File f= new File(path,"/Camera/test.mp4");
		
		//此时的f.getAbsolutePath()= /storage/emulated/0/DCIM/Camera/test.mp4
		videoView.setVideoPath(f.getAbsolutePath());
		
		//VideoView获焦点
		// videoView.requestFocus();
		
		Button buttonStart=(Button) findViewById(R.id.start);
		buttonStart.setOnClickListener(this);
		
		Button buttonPause=(Button) findViewById(R.id.pause);
		buttonPause.setOnClickListener(this);
		
		Button buttonSeekTo=(Button) findViewById(R.id.seekto);
		buttonSeekTo.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		
		switch(v.getId()){
		case R.id.start:
			// 开始播放视频
			videoView.start();
			break;
			
		case R.id.pause:
			videoView.pause();
			break;
			
		case R.id.seekto:
			videoView.seekTo(2000);
			break;
		}
	}
}

<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秒" />
        
        <Button
            android:id="@+id/fromIntent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="启动Intent" />
    </LinearLayout>

</FrameLayout>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值