2014-10-20 167 views
-1

我如何获得一个YouTube视频是播放列表 示例的一部分的ID像这样的链接:如何从PlaylistID获取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(); 
     } 
    } 
} 

}

回答

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 
} 
+0

我真的只是一个方法来提取这一行kXYiU_JCYtU表示该https://www.youtube.com/watch?v=kXYiU_JCYtU&list=PL87FA01F68C540290 – john 2014-10-20 15:30:50

+0

然后,只需解析字符串'url.split(“&” )[0]' – 2014-10-20 15:32:05

+0

抱歉,我该怎么办? :) – john 2014-10-20 15:34:53