Search code examples
androidvideostreamingmedia-playerandroid-videoview

Android: app forces close when trying to play video from URI


I have written this code to try and stream a video to my Android device. Upon clicking an icon, a popup window should appear with the VideoView inside it. However, whenever I click the image to trigger the popup, it forces closes. The URL I'm using is apparently able to be streamed to a device, I got it from another SO question. This is the code in my class.

    video.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            final Dialog dialog = new Dialog(context);
            dialog.setContentView(R.layout.pop_up);
            dialog.setTitle("Safe Cracker Overview");

            VideoView vv = (VideoView)findViewById(R.id.vv01);
            MediaController mc = new MediaController(context);
            mc.setAnchorView(vv);
            mc.setMediaPlayer(vv);
            vv.setMediaController(mc);
            vv.setVideoURI(Uri.parse("http://hermes.sprc.samsung.pl/widget/tmp/testh.3gp"));
            vv.requestFocus();
            vv.start();
            Button ok = (Button)findViewById(R.id.but01);

            ok.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    dialog.dismiss();
                }
            });

            dialog.show();
        }
    });

And the XML for the popup layout:

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

    <VideoView
        android:id="@+id/vv01"
        android:layout_width="fill_parent"
        android:layout_height="200dp"
        android:layout_marginTop="20dp"
        android:background="#0000" >
    </VideoView>

    <Button
        android:id="@+id/but01"
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:layout_gravity="center"
        android:layout_marginTop="10dp"
        android:background="@drawable/green_button"
        android:text="@string/back"
        android:textColor="@color/white"
        android:textSize="16dp" >
    </Button>

</LinearLayout>

Can anyone see the issue here? Thanks


Solution

  • change

    VideoView vv = (VideoView)findViewById(R.id.vv01);

    with

    VideoView vv = (VideoView)dialog .findViewById(R.id.vv01);

    and Button ok = (Button)findViewById(R.id.but01); with Button ok = (Button)dialog .findViewById(R.id.but01);