Deepak Borade
Deepak Borade

Reputation: 301

How to Play Video In ListView in Android

I want to play video in my application. I created a custom ListView with VideoView. The VideoView displayed successfully but one problem is that when I am move list view VideoView changed its position. This is not looking good in my application. My requirement is that when click on button video should start played in that area of list view only. How could I achieve this?

Here is code of displaying VideoView in ListView:

public class MyVideoAdapter extends BaseAdapter {

    Context context;
    String a[];
    LayoutInflater inflate;
    VideoView vv;
    ImageButton img;

    public MyVideoAdapter(Context context, String[] a) {
        this.context = context;
        this.a = a;
        inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount() {
        return a.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View v = convertView;
        if (convertView == null)
            v = inflate.inflate(R.layout.row, null);
        img = (ImageButton) v.findViewById(R.id.imageButton1);
        String ans = a[position];

        if (ans.equals("one")) {
            vv = (VideoView) v.findViewById(R.id.VideoView);
            MediaController mc = new MediaController(context);
            mc.setAnchorView(vv);
            mc.setMediaPlayer(vv);
            // Uri uri=Uri.parse("https://www.dropbox.com/s/d5blvemu8yse38s/kesari.mp4");
            // vv.setMediaController(mc);
            String path = "android.resource://com.example.wealthcreation/raw/kesari";
            vv.setVideoURI(Uri.parse(path));

            img.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    img.setVisibility(View.INVISIBLE);
                    vv.start();

                }
            });

            // img.setImageResource(R.drawable.k1);
            // ImageButton img=(ImageButton)v.findViewById(R.id.imageButton1);
            // Button btn1=(Button)v.findViewById(R.id.btnplay);

            /*
            img.setOnClickListener(new OnClickListener()
            {

            @Override
            public void onClick(View v)
            {
                vv.setVisibility(View.VISIBLE);
                img.setVisibility(View.GONE);
                MediaController mc = new MediaController(context);
                mc.setAnchorView(vv);
                mc.setMediaPlayer(vv);
                // Uri uri=Uri.parse("https://www.dropbox.com/s/d5blvemu8yse38s/kesari.mp4");
                vv.setMediaController(mc);
                String path = "android.resource://com.example.wealthcreation/raw/kesari";
                vv.setVideoURI(Uri.parse(path));
                vv.start();
                String videoId = "CUuBdtsQx6U";
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
                        + videoId));
                intent.putExtra("VIDEO_ID", videoId);
                context.startActivity(intent);
                // Toast.makeText(context, "Hii",Toast.LENGTH_LONG).show();
                // this intent define an action and and parse the url
                // context.startActivity(new
                // Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=CUuBdtsQx6U")));

                /*
                WebView webview = new WebView(context);

                String htmlString = "<html> <body> <embed src=\"youtube link\"; type=application/x-shockwave-flash width="
                        + widthOfDevice
                        + " height="
                        + heightOfDevice
                        + "> </embed> </body> </html>";

                webview.loadData(htmlString, "text/html", "UTF-8");

                try
                {
                    MediaPlayer mp = new MediaPlayer();
                    mp.setDataSource("http://www.youtube.com/watch?v=CUuBdtsQx6U");
                    mp.prepare();
                    mp.start();
                }
                catch (Exception e)
                {
                    Log.d("videos", "", e);
                }
            }

            });
            */

        }

        if (ans.equals("two"))
        {
            /*
            img.setImageResource(R.drawable.k2);
            Button btn2 = (Button) v.findViewById(R.id.btnplay);

            btn2.setOnClickListener(new OnClickListener()
            {

            @Override
            public void onClick(View v)
            {
                Toast.makeText(context, "Hii", Toast.LENGTH_LONG).show();
                // context.startActivity(new
                // Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=f6fmZFDE5EA")));
                // this intent define an action and and parse the url
                // context.startActivity(new
                // Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=CUuBdtsQx6U")));
                String videoId = "f6fmZFDE5EA";
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
                        + videoId));
                intent.putExtra("VIDEO_ID", videoId);
                context.startActivity(intent);
                // context.startActivity(new
                // Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?feature=player_detailpage&v=f6fmZFDE5EA")));
            }

            });
            */
        }

        if (ans.equals("three"))
        {
            /*
            Button btn3 = (Button) v.findViewById(R.id.btnplay);

            btn3.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    String videoId = "f6fmZFDE5EA";
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
                            + videoId));
                    intent.putExtra("VIDEO_ID", videoId);
                    context.startActivity(intent);

                    // Toast.makeText(context, "three",Toast.LENGTH_LONG).show();
                    // context.startActivity(new
                    // Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=f6fmZFDE5EA")));
                    // this intent define an action and and parse the url
                    // context.startActivity(new
                    // Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=CUuBdtsQx6U")));
                }

            });
            */
        }
        if (ans.equals("four"))
        {
            /*
            img.setImageResource(R.drawable.k4);
            Button btn4 = (Button) v.findViewById(R.id.btnplay);

            btn4.setOnClickListener(new OnClickListener()
            {

            @Override
            public void onClick(View v)
            {
                String videoId = "VBMuzOrVD18";
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
                        + videoId));
                intent.putExtra("VIDEO_ID", videoId);
                context.startActivity(intent);

                // Toast.makeText(context, "four",Toast.LENGTH_LONG).show();
                // context.startActivity(new
                // Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=VBMuzOrVD18")));
                // this intent define an action and and parse the url
                // context.startActivity(new
                // Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=CUuBdtsQx6U")));
            }

            });
            */
        }
        if (ans.equals("fifth"))
        {
            /*
            img.setImageResource(R.drawable.k5);
            Button btn5 = (Button) v.findViewById(R.id.btnplay);
            btn5.setOnClickListener(new OnClickListener()
            {

                @Override
                public void onClick(View v)
                {
                    String videoId = "-UlSAYhHqqc";
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:"
                            + videoId));
                    intent.putExtra("VIDEO_ID", videoId);
                    context.startActivity(intent);

                    // Toast.makeText(context, "fith",Toast.LENGTH_LONG).show();
                    // context.startActivity(new
                    // Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=-UlSAYhHqqc")));
                    // this intent define an action and and parse the url
                    // context.startActivity(new
                    // Intent(Intent.ACTION_VIEW,Uri.parse("http://www.youtube.com/watch?v=CUuBdtsQx6U")));
                }

            });
            */
        }
        return v;
    }

}

Upvotes: 2

Views: 14344

Answers (2)

SID --- Choke_de_Code
SID --- Choke_de_Code

Reputation: 1141

In your case,You have already parse your data in XML format so difinately you have to store all your data in HashMap Arraylist with particular key for your Video url.

Now, what can do, If you need to open Video on Item Click of ListView then you can retrieve your video url in String like

String  myUrl = urHashMaparraylist.get(position).get("videolocation");

Now pass this myUrl to next Activity and just set this String as a

 Uri video = Uri.parse(myUrl);
 videoView.setVideoURI(video);

listView.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {

        String  myUrl = urHashMaparraylist.get(position).get("videolocation");
        Intent n = new Intent(YourActivityName.this , NewActivityName.class);
        n.putExtra("videolocation",myUrl);
        startActivity(n);

    }

});

Now in your next Activity retrieve it as a

Uri video = Uri.parse(url);
videoView.setVideoURI(video);

For more reference for video streaming ... you can check the following url: http://developer.samsung.com/android/technical-docs/Android-Media-Streaming-Tutorial you can even check my answer at How Do I Play Video in ListView like Instagram and Vine?

Upvotes: 1

user3367142
user3367142

Reputation: 1

I was stumped with this too until I found this post: https://stackoverflow.com/a/6832598/3367142

In short set a transparent background for your VideoView

android:background="#0000"

Upvotes: 0

Related Questions