videoview播放當前項目res/raw下視頻



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="mathc_parent"
android:orientation="vertical" >

<com.demo.CustomVideoView //本身寫這個類,是能夠固定videoview大小
android:id=「+id/videoview"
android:layout_width="match_parent"
android:layout_height="march_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height+"match_parent"
android:orientation="horizontal"
android:gravity="center">
    <Button        //三個button 播放,暫停,重播
    android:id="+id/bt1"
    .../>
    
    <Button
    android:id="+id/bt2"
    .../>
    <Button
    android:id="+id/bt3"
    .../>
    </LinearLayout>
 </LinearLayout>

class CustomVideoViewjava

public class CustomVideoView extends VideoView{
    public CustomVideoView(Context context) {
        super(context);
        }
    public CustomVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        }
     public CustomVideoView(Context context,AttributeSet attrs, int defStyle) {
     super(context, attrs, defStyle);
     }
     @Override
     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
        //this.setMeasuredDimension(1024, 420);//這個是設置videoview大小,
        }
       }

Activityandroid

1 在oncreate裏 initview()  初始化ide

2 private void initview(){this

vv = (CustomVideoView) view.findViewById(R.id.videoView);code

vv.setVideoURI(Uri.parse("android.resource://"+getPackageName() +"/"+R.raw.test));//這是播放在項目裏的視頻,要放在 res/raw下 test是視頻名
視頻

play = (Button) view.findViewById(R.id.play);xml

pause = (Button) view.findViewById(R.id.pause);utf-8

replay = (Button) view.findViewById(R.id.replay);get

play.setOnClickListener(new MyClickListener());it

pause.setOnClickListener(new MyClickListener());

replay.setOnClickListener(new MyClickListener());

}

class MyClickListener implements OnClickListener {

@Override

public void onClick(View v) {

    switch (v.getId()) {

        case R.id.play:

            if (!vv.isPlaying()) {

                vv.start(); // 開始播放

            }

                    break;

        case R.id.pause:    

            if (vv.isPlaying()) { vv.pause(); // 暫時播放} break;

        case R.id.replay:

            if (vv.isPlaying()) {vv.resume(); // 從新播放} break;

            default:break;

相關文章
相關標籤/搜索