如何在自定义视图类中播放暂停视频视图?

时间:2018-09-04 07:52:18

标签: android android-videoview

我创建了一个扩展FrameLayout的CustomView类,以设计用于videoView的自定义媒体控制器。现在这很简单,我将在以后完成。

我在MainActivity中有一个VideoView,在CustomView类中有另一个,并且我在CustomView中使用setVideoView方法访问MainActivity中的videoView来暂停它。

但是,当我单击暂停按钮时,该应用程序将关闭。我是Android的初学者。

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        videoView=findViewById(R.id.videoView);
        Uri video = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video);
        videoView.setVideoURI(video);
        videoView.start();
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <VideoView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:id="@+id/videoView"/>

    <com.example.arantik.test13.CustomView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/customView" />

</LinearLayout>

CustomView.java:

public class CustomView extends FrameLayout {

    protected Button pauseButton;
    VideoView videoView;

    public CustomView(@NonNull Context context) {
        super(context);
        init(context, null);
    }

    public CustomView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public CustomView(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs);
    }

    protected OnClickListener pauseOnClick(final Context context) {
        return new OnClickListener() {
            @Override
            public void onClick(View view) {
                videoView.pause();
            }
        };
    }

    public void setVideoView(VideoView videoView) {
        this.videoView = videoView;
    }

    public void init(Context context, AttributeSet attributeSet){
        inflate(context, R.layout.custom_view, this);
        pauseButton = findViewById(R.id.pauseButton);
        pauseButton.setOnClickListener(playOnClick(context));
    }
}

custom_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="#ff0000">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal">

        <Button
            android:layout_width="35dp"
            android:layout_height="35dp"
            android:id="@+id/pauseButton"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"/>

    </FrameLayout>

</LinearLayout>

和项目崩溃后的Logcat:

09-03 10:59:18.980 1646-1646/com.example.arantik.test13 E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.arantik.test13/com.example.arantik.test13.MainActivity}: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.arantik.test13.CustomView
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
        at android.app.ActivityThread.access$600(ActivityThread.java:141)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #11: Error inflating class com.example.arantik.test13.CustomView
        at android.view.LayoutInflater.createView(LayoutInflater.java:620)

1 个答案:

答案 0 :(得分:-2)

它应该可以帮助您....

public class MainActivity extends AppCompatActivity {
@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        VideoView videoView = (VideoView) findViewById(R.id.videoView);
        videoView.setVideoPath("android.resource://" + getPackageName() + "/" + R.raw.demovideo);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
        videoView.setMediaController(mediaController);
        videoView.start();
    }
}