使用php从mysql数据库显示youtube视频?

时间:2016-10-11 18:17:57

标签: php mysql video iframe youtube

我想在页面上显示youtube视频,以下是我的代码,以便从存储在mysql中的数据库中获取youtube视频URL,然后通过迭代使用while循环显示它。但是,iframe不会在其中显示任何视频。

   @Override
    protected void onPostExecute(String result) {

        JSONObject jsonObject;
        JSONArray jsonArray;
        CustomAdapter customAdapter;
        ListView listview;

        listview = (ListView) findViewById(R.id.xlistview);
        customAdapter = new CustomAdapter(getApplicationContext(), R.layout.row_layout);
        listview.setAdapter(customAdapter);
                                                                                 //Log.d("json_raw_home",result);

        if (result != null) {                                                    //Log.d("post execute",result);
            try {

                jsonObject = new JSONObject(result);
                jsonArray = jsonObject.getJSONArray("data");
                int count = 0;
                String subject, description;
                while (count < jsonArray.length()) {
                    JSONObject JO = jsonArray.getJSONObject(count);
                    subject = JO.getString("subject");
                    description = JO.getString("description");

                    Converter converter = new Converter(subject, description);
                    customAdapter.add(converter);

                    count++;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


        }

        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String item = String.valueOf(parent.getItemAtPosition(position));
                Toast.makeText(getApplicationContext(),item, Toast.LENGTH_LONG).show();}
        });
    }
}

public class CustomAdapter extends ArrayAdapter {
    List list = new ArrayList();

    public CustomAdapter(Context context, int resource) {
        super(context, resource);
    }

    @Override
    public void add(Object object) {
        super.add(object);
        list.add(object);
    }

    @Override
    public int getCount() {
        return list.size();
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row;
        row = convertView;
        ContactHolder contactHolder;

        if(row==null)
        {
            LayoutInflater layoutInflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = layoutInflater.inflate(R.layout.row_layout,parent,false);
            contactHolder=new ContactHolder();
            contactHolder.tv_subject=(TextView)row.findViewById(R.id.xsubject_tv);
            contactHolder.tv_description=(TextView)row.findViewById(R.id.xdescription_tv);
            row.setTag(contactHolder);
        }

        else
        {
            contactHolder=(ContactHolder)row.getTag();
        }

        Converter converter = (Converter) this.getItem(position);
        contactHolder.tv_subject.setText(converter.getSubject());
        contactHolder.tv_description.setText(converter.getDescription());
        return row;
    }

    public class ContactHolder{
        TextView tv_subject,tv_description;
    }


}

public class Converter {
    private String subject,description;

    public Converter(String subject, String description) {
        this.setSubject(subject);
        this.setDescription(description);
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }
}

我也试过在循环中使用视频标签,但它不会在视频播放器中显示Youtube视频。 请帮我解决这个问题。

2 个答案:

答案 0 :(得分:1)

您可以将Youtube与嵌入一起使用,如下所示:

<iframe width="560" height="315" src="https://www.youtube.com/embed/<?php echo $row["url"]; ?>?autoplay=1&autohide=1&controls=1&showinfo=0&modestbranding=1&rel=0"></iframe>

答案 1 :(得分:0)

问题在于您的网址值。使用"watch?v=在视频网址中放置"v/"“并重试。例如,我尝试了

<iframe width="560" height="315" 
src="https://www.youtube.com//v/txSGdNONUJE" frameborder="0" allowfullscreen></iframe> 

以上代码可以正常使用。