Android videoView 不显示内部片段

发布于 2025-01-11 02:40:41 字数 7865 浏览 0 评论 0原文

嘿,我在尝试将包含视频视图的片段调用到当前片段页面时遇到了一些问题。

当我单击测试按钮时,我设置它会毫无错误地执行代码,但从未显示视频片段 - 我什至给它设置了红色背景色,这样我就可以看到它加载到我当前的视图中。

我的目标只是将视频路径的参数发送到视频片段 videoview 播放器并开始播放。

videoPlay.java

public class videoPlay extends Fragment {
    public videoPlay(){
    //constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout._fragvideoplay, container, false);

        MediaController mc= new MediaController(getActivity());
        VideoView view = (VideoView)rootView.findViewById(R.id.videoView);
        String path = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/sddir/Bubble Guppies.mp4";
        view.setVideoURI(Uri.parse(path));
        view.setMediaController(mc);
        view.start();

        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    }
}

_fragvideoplayer.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

FragMovies.java:

public class FragMovies extends Fragment {

    public FragMovies(){
    //constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout._fragmovies, container, false);
        WebView view = (WebView) rootView.findViewById(R.id.webView);

        view.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

                container.findViewById(R.id.webView).setVisibility(View.GONE);
                container.findViewById(R.id.pBar1).setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                container.findViewById(R.id.pBar1).setVisibility(View.GONE);
                container.findViewById(R.id.webView).setVisibility(View.VISIBLE);
            }
        });

        view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        view.getSettings().setJavaScriptEnabled(true);
        view.getSettings().setAllowContentAccess(true);
        view.getSettings().setAllowFileAccess(true);
        view.getSettings().setLoadsImagesAutomatically(true);
        view.getSettings().setAllowFileAccess(true);
        view.getSettings().setBuiltInZoomControls(false);
        view.getSettings().setDomStorageEnabled(true);
        view.getSettings().setAppCacheEnabled(true);
        view.getSettings().setDisplayZoomControls(false);
        view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        view.getSettings().setSupportZoom(false);

        view.setHorizontalScrollBarEnabled(false);
        view.setVerticalScrollBarEnabled(false);
        view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        view.loadUrl("https://www.bing.com/");

        return rootView ;
    }
}

_fragmovies.xml:

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

    <ProgressBar
        android:id="@+id/pBar1"
        android:layout_width="93dp"
        android:layout_height="match_parent"
        android:layout_alignParentTop="false"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="950px"
        android:layout_marginLeft="950px" />

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" />    
</LinearLayout>

Main_activity.java:

public class MainActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;
    PagerAdapter pagerAdapter;
    Button button;

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

        getViews();

        //adapter setup
        pagerAdapter = new com.example.telluridetainment.PagerAdapter(getSupportFragmentManager());

        //attaching fragments to adapter
        pagerAdapter.addFragment(new FragMovies(),"Movies");

        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

        //setting icons
        tabLayout.getTabAt(0).setIcon(R.drawable.ic_movie_white_24dp);

        button = (Button) findViewById(R.id.button0);
        button.setOnClickListener(new MyClass());
    }

    public class MyClass implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            videoPlay myfragment = new videoPlay();
            //pass data
            Bundle bundle = new Bundle();
            bundle.putString("KEY","DATA");
            myfragment.setArguments(bundle);

            FragmentTransaction fragmentManager = getSupportFragmentManager().beginTransaction();
            fragmentManager.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

            fragmentManager.addToBackStack(null);
            fragmentManager.replace(R.id.viewPager, myfragment).commit();
        }

    }

    private void getViews() {
        tabLayout = findViewById(R.id.mTabLayout);
        viewPager = findViewById(R.id.viewPager);
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="2000px"
    android:layout_height="1200px"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button0"
        android:layout_width="200dp"
        android:layout_height="152dp"
        android:text="Button"
        android:textColor="#B71C1C" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/appbarLayout"
        tools:ignore="SpeakableTextPresentCheck">
    </android.support.v4.view.ViewPager>
</RelativeLayout>

电影的路径是正确的。电影位于“外部”SD 卡路径中。单击新片段(videoPlay)不会产生视频菜单(播放、提醒、暂停...),因此它不存在。

任何帮助都会很棒!

Hey all I am having a little bit of an issue with trying to call a fragment that has the videoview within it into my current fragment page.

When I click on the test button I set up it goes through the code without error but never shows the video fragment - I even gave it a background color of red so I could see it load into my current view.

My goal here is just to send a parameter of the video path to the video fragment videoview player and start playing it.

videoPlay.java

public class videoPlay extends Fragment {
    public videoPlay(){
    //constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout._fragvideoplay, container, false);

        MediaController mc= new MediaController(getActivity());
        VideoView view = (VideoView)rootView.findViewById(R.id.videoView);
        String path = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/sddir/Bubble Guppies.mp4";
        view.setVideoURI(Uri.parse(path));
        view.setMediaController(mc);
        view.start();

        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

    }
}

_fragvideoplayer.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />
</LinearLayout>

FragMovies.java:

public class FragMovies extends Fragment {

    public FragMovies(){
    //constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout._fragmovies, container, false);
        WebView view = (WebView) rootView.findViewById(R.id.webView);

        view.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);

                container.findViewById(R.id.webView).setVisibility(View.GONE);
                container.findViewById(R.id.pBar1).setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                container.findViewById(R.id.pBar1).setVisibility(View.GONE);
                container.findViewById(R.id.webView).setVisibility(View.VISIBLE);
            }
        });

        view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        view.getSettings().setJavaScriptEnabled(true);
        view.getSettings().setAllowContentAccess(true);
        view.getSettings().setAllowFileAccess(true);
        view.getSettings().setLoadsImagesAutomatically(true);
        view.getSettings().setAllowFileAccess(true);
        view.getSettings().setBuiltInZoomControls(false);
        view.getSettings().setDomStorageEnabled(true);
        view.getSettings().setAppCacheEnabled(true);
        view.getSettings().setDisplayZoomControls(false);
        view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        view.getSettings().setSupportZoom(false);

        view.setHorizontalScrollBarEnabled(false);
        view.setVerticalScrollBarEnabled(false);
        view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        view.loadUrl("https://www.bing.com/");

        return rootView ;
    }
}

_fragmovies.xml:

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

    <ProgressBar
        android:id="@+id/pBar1"
        android:layout_width="93dp"
        android:layout_height="match_parent"
        android:layout_alignParentTop="false"
        android:layout_centerHorizontal="true"
        android:layout_marginStart="950px"
        android:layout_marginLeft="950px" />

    <WebView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true" />    
</LinearLayout>

Main_activity.java:

public class MainActivity extends AppCompatActivity {
    TabLayout tabLayout;
    ViewPager viewPager;
    PagerAdapter pagerAdapter;
    Button button;

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

        getViews();

        //adapter setup
        pagerAdapter = new com.example.telluridetainment.PagerAdapter(getSupportFragmentManager());

        //attaching fragments to adapter
        pagerAdapter.addFragment(new FragMovies(),"Movies");

        viewPager.setOffscreenPageLimit(1);
        viewPager.setAdapter(pagerAdapter);
        tabLayout.setupWithViewPager(viewPager);

        //setting icons
        tabLayout.getTabAt(0).setIcon(R.drawable.ic_movie_white_24dp);

        button = (Button) findViewById(R.id.button0);
        button.setOnClickListener(new MyClass());
    }

    public class MyClass implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            videoPlay myfragment = new videoPlay();
            //pass data
            Bundle bundle = new Bundle();
            bundle.putString("KEY","DATA");
            myfragment.setArguments(bundle);

            FragmentTransaction fragmentManager = getSupportFragmentManager().beginTransaction();
            fragmentManager.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

            fragmentManager.addToBackStack(null);
            fragmentManager.replace(R.id.viewPager, myfragment).commit();
        }

    }

    private void getViews() {
        tabLayout = findViewById(R.id.mTabLayout);
        viewPager = findViewById(R.id.viewPager);
    }
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="2000px"
    android:layout_height="1200px"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button0"
        android:layout_width="200dp"
        android:layout_height="152dp"
        android:text="Button"
        android:textColor="#B71C1C" />

    <android.support.v4.view.ViewPager
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/appbarLayout"
        tools:ignore="SpeakableTextPresentCheck">
    </android.support.v4.view.ViewPager>
</RelativeLayout>

The path to the movie is correct. And the movie is there in the "external" SD card path. Clicking around on the new fragment (videoPlay) yields no video menu (play, remind, pause...) so its not there.

Any help would be great!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文