如何从PlaylistID获取YouTube视频ID?

时间:2014-10-20 14:42:48

标签: java android youtube youtube-api playlist

如何获取属于播放列表的YouTube视频的ID 例如这样的链接:

https://www.youtube.com/watch?v=kXYiU_JCYtU&list=PL87FA01F68C540290

这是gdata

http://gdata.youtube.com/feeds/api/playlists/PL87FA01F68C540290?v=2&alt=jsonc&max-results=50

这是我的代码

public class Episodi extends Activity implements OnClickListener {

ImageView mainThumb;
TextView mainTitle;
TextView mainTime;
LinearLayout videos;
ArrayList<String> links;
URL jsonURL;


/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.episodi);

    new ParseVideoDataTask().execute();
    mainThumb = (ImageView) findViewById(R.id.mainThumb);
    mainTitle = (TextView) findViewById(R.id.mainTitle);
    mainTime = (TextView) findViewById(R.id.mainTime);
    videos = (LinearLayout) findViewById(R.id.videos);

}

public class ParseVideoDataTask extends AsyncTask<String, String, String> {
    int count = 0;

    @Override
    protected String doInBackground(String... params) {
        URL jsonURL = null;
        URLConnection jc;
        links = new ArrayList<String>();
        try {

                jsonURL = new URL("http://gdata.youtube.com/feeds/api/playlists/" +
                "PL87FA01F68C540290" +  
                "?v=2&alt=jsonc&max-results=50");


             jc = jsonURL.openConnection(); 
             InputStream is = jc.getInputStream(); 
             String jsonTxt = IOUtils.toString(is); 
             JSONObject jj = new JSONObject(jsonTxt); 
             JSONObject jdata = jj.getJSONObject("data"); 
             JSONArray aitems = jdata.getJSONArray("items"); 
             for (int i=0;i<aitems.length();i++) {
                 JSONObject item = aitems.getJSONObject(i); 
                 JSONObject video = item.getJSONObject("video"); 
                 String title = video.getString("title");
                 JSONObject player = video.getJSONObject("player");
                 String link = player.getString("default");
                 String length = video.getString("duration");
                 JSONObject thumbnail = video.getJSONObject("thumbnail"); 
                 String thumbnailUrl = thumbnail.getString("hqDefault");
                 String[] deets = new String[4];
                 deets[0] = title;
                 deets[1] = thumbnailUrl;
                 deets[2] = length;
                 links.add(link);
                 publishProgress(deets);
             }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }       
        return null;
    }       



    @Override
    protected void onProgressUpdate(final String... deets) {
        count++;
        if (count == 1) {
            Picasso.with(getBaseContext()).load(deets[1]).into(mainThumb);
            mainTitle.setText(deets[0]);
            mainTime.setText(formatLength(deets[2]));
            mainThumb.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(Episodi.this,Video.class);
                    //I want to insert .putExtra here for the VIDEO ID (example. kXYiU_JCYtU)
                    startActivity(i);
                }
            });
        } else {
            LayoutInflater layoutInflater = (LayoutInflater)Episodi.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View video = layoutInflater.inflate(R.layout.videothumb, null);
            ImageView thumb = (ImageView) video.findViewById(R.id.thumb);
            TextView title = (TextView) video.findViewById(R.id.title);
            TextView time = (TextView) video.findViewById(R.id.time);
            Picasso.with(getBaseContext()).load(deets[1]).into(thumb);
            title.setText(deets[0]);
            time.setText(formatLength(deets[2]));
            video.setPadding(20, 20, 20, 20);
            videos.addView(video);
            video.setId(count-1);
            video.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                    Intent i = new Intent(Episodi.this,Video.class);
                    //I want to insert .putExtra here for the VIDEO ID (example. kXYiU_JCYtU)
                    startActivity(i);
                }
            });
        }
    }
}

private CharSequence formatLength(String secs) {
    int secsIn = Integer.parseInt(secs);
    int hours = secsIn / 3600,
            remainder = secsIn % 3600,
            minutes = remainder / 60,
            seconds = remainder % 60;

            return ((minutes < 10 ? "0" : "") + minutes
            + ":" + (seconds< 10 ? "0" : "") + seconds );
}

public void onDestroy() {
    super.onDestroy();
    for (int i=0;i<videos.getChildCount();i++) {
        View v = videos.getChildAt(i);
        if (v instanceof ImageView) {
            ImageView iv = (ImageView) v;           
            ((BitmapDrawable)iv.getDrawable()).getBitmap().recycle(); 
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

正如我在这里看到的那样,你将JSON格式化的字符串作为回应。

有许多JSON API,您可以在Android中使用。只需制作JSONObject并获取您感兴趣的数据。

JSONObject playlist = new JSONObject(response);
// list items
JSONArray items = playlist.getJSONObject("data").getJSONArray("items");
// traverse items array and get video url
for(int i=0; i<items.length(); i++) {
    JSONObject item = items.getJSONObject(i);
    // video url
    String videoURL = item.getJSONObject("video").getJSONObject("player").getString("default");
}

你需要在try-catch块中包装所有这些。

修改

据我了解,您需要解析列表links中的字符串。

for(String link : links) {
    String id = link.split("&")[0].split("v=")[1];
    // do what you need with the link
}