赞
踩
video.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.video1);
创建一个MediaController对象,然后在VideoView方法中去设置
MediaController mediaController=new MediaController(MainActivity.this);
video.setMediaController(mediaController);
- package com.example.exam_01;
-
- import androidx.appcompat.app.AppCompatActivity;
-
- import android.os.Bundle;
- import android.view.View;
- import android.widget.ImageView;
- import android.widget.MediaController;
- import android.widget.VideoView;
-
- public class MainActivity extends AppCompatActivity{
- ImageView imageView;
- VideoView video;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- video=this.findViewById(R.id.vv1);
- video.setVideoPath("android.resource://"+getPackageName()+"/"+R.raw.video1);
- MediaController mediaController=new MediaController(MainActivity.this);
- video.setMediaController(mediaController);
-
- imageView=this.findViewById(R.id.ivv1);
- imageView.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- play();
- }
- });
- video.start();
- }
-
-
-
- public void play(){
- if(video.isPlaying()&&video!=null){
- imageView.setImageResource(android.R.drawable.ic_media_pause);
- video.pause();
- return;
- }
- imageView.setImageResource(android.R.drawable.ic_media_play);
- video.start();
-
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- >
-
- <VideoView
- android:id="@+id/vv1"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_centerInParent="true"
- />
- <ImageView
- android:layout_width="80dp"
- android:layout_height="80dp"
- android:id="@+id/ivv1"
- android:layout_alignParentBottom="true"
- android:layout_centerHorizontal="true"
- android:layout_marginBottom="60dp"
- android:src="@android:drawable/ic_media_play"/>
-
- </RelativeLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。