如何在Android中的ListView中播放视频

时间:2013-12-31 11:41:30

标签: android listview adapter android-videoview

我想在我的应用程序中播放视频。我使用ListView创建了自定义VideoViewVideoView显示成功,但有一个问题是,当我移动列表视图VideoView时,它会改变其位置。这在我的应用程序中看起来不太好。我的要求是,点击button视频时,只能在列表视图的该区域播放。我怎么能做到这一点?

以下是在VideoView中显示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;
    }

}

1 个答案:

答案 0 :(得分:0)

直到我发现这篇文章之后,我才对此感到难过: https://stackoverflow.com/a/6832598/3367142

简而言之,为您的VideoView设置透明背景

机器人:背景=&#34;#0000&#34;