Как воспроизвести паузу просмотра видео в пользовательском классе просмотра? - PullRequest
0 голосов
/ 04 сентября 2018

Я создал класс CustomView, расширяющий FrameLayout, для разработки своего собственного медиа-контроллера для videoView. Теперь это очень просто, и я собираюсь завершить это позже.

У меня есть VideoView в MainActivity и еще один в классе CustomView, и я использую метод setVideoView в CustomView для доступа к videoView в MainActivity, чтобы приостановить его.

Но когда я нажимаю кнопку паузы, приложение закрывается. Я новичок в 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 голосов
/ 13 декабря 2018

Это должно помочь вам ....

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();
    }
}
...