项目地址:点击打开链接
下面用一个我自己写的视屏控件组合来说明
图
代码:这是控件组合的代码
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/li_video"> <RelativeLayout android:layout_width="match_parent" android:layout_height="200dp"> <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:background="#000000"> <VideoView android:id="@+id/video" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" /> </RelativeLayout> <ImageView android:id="@+id/video_thumbnail" android:layout_width="match_parent" android:layout_height="match_parent" /> <Button android:id="@+id/button_play" android:layout_width="64dp" android:layout_height="64dp" android:layout_centerInParent="true" android:background="@mipmap/video_play" /> <ImageView android:id="@+id/to_be_bigger" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="@mipmap/video_tobebigger" /> </RelativeLayout> </LinearLayout>
写一个类来控制你的控件:
package com.yyx.customcontrol.widget; import android.content.Context; import android.content.res.TypedArray; import android.media.MediaPlayer; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.Toast; import android.widget.VideoView; import com.bumptech.glide.Glide; import com.yyx.customcontrol.R; /** * Created by yyx 2017/3/24. */ public class VideosView extends LinearLayout { private Button play; private VideoView videoView; private String videoUrl; private String thumbnail; private ImageView thumbnailImage; public VideosView(Context context) { super(context); } public VideosView(final Context context, @Nullable AttributeSet attrs) { super(context, attrs); LayoutInflater.from(context).inflate(R.layout.activity_video, this); TypedArray ta = getContext().obtainStyledAttributes(attrs,R.styleable.VideosView); videoUrl = ta.getString(R.styleable.VideosView_video_url); //获取在attrs文件中定义的VideosView的videourl的值 thumbnail = ta.getString(R.styleable.VideosView_thumbnail_url); videoView = (VideoView) findViewById(R.id.video); play = (Button) findViewById(R.id.button_play); thumbnailImage = (ImageView) findViewById(R.id.video_thumbnail); play.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(context,videoUrl,Toast.LENGTH_LONG).show(); videoView.setVisibility(View.VISIBLE); //videoImage.setVisibility(View.GONE); videoView.setVideoPath(videoUrl); if (videoView.isPlaying()) { videoView.pause(); play.setVisibility(View.VISIBLE); } else { videoView.start(); play.setVisibility(View.GONE); } } }); videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { play.setVisibility(View.GONE); Glide.with(context) .load(R.drawable.video_error) .into(thumbnailImage); thumbnailImage.setVisibility(View.VISIBLE); return true; } }); findViewById(R.id.li_video).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { videoView.pause(); play.setVisibility(View.VISIBLE); } }); //视频缩略图 if(thumbnail != null) { Glide.with(context) .load(thumbnail) .error(R.drawable.default_video) .placeholder(R.drawable.default_video) .into(thumbnailImage); } videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { play.setVisibility(View.VISIBLE); } }); } public String getThumbnail() { return thumbnail; } public void setThumbnail(String thumbnail) { this.thumbnail = thumbnail; } public String getVideoUrl() { return videoUrl; } public void setVideoUrl(String videoUrl) { this.videoUrl = videoUrl; } public VideosView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } }
给你的控件定义属性:
<?xml version="1.0" encoding="utf-8"?> <resources> <!--给VideosView定义属性--> <declare-styleable name="VideosView"> <attr name="video_url" format="string"/> <attr name="thumbnail_url" format="string"/> </declare-styleable> </resources>
在你需要用控件的xml调用控件:
<com.yyx.customcontrol.widget.VideosView android:id="@+id/vv" android:layout_width="match_parent" android:layout_height="match_parent" app:video_url="视频路径" app:thumbnail_url="视频缩略图路径" >
最后在你所调用的activity给属性赋值就行了:
package com.yyx.customcontrol; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import com.yyx.customcontrol.widget.VideosView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); VideosView videosView = (VideosView) findViewById(R.id.vv); videosView.setVideoUrl("456"); } }
注:
VideosView videosView = (VideosView) findViewById(R.id.vv); videosView.setVideoUrl("456");
如果不这样给属性赋值的话,他就取下面赋的,不过上面的优先级最高:
<com.yyx.customcontrol.widget.VideosView android:id="@+id/vv" android:layout_width="match_parent" android:layout_height="match_parent" app:video_url="视频路径" app:thumbnail_url="视频缩略图路径" >