2012-01-12 98 views
14

Trong Hoạt động trên Android của tôi, tôi muốn phát video từ YouTube thông qua ứng dụng YouTube hoặc một số ứng dụng khác. Tôi muốn tải hình thu nhỏ của video trong hoạt động của mình.Cách tạo hình thu nhỏ video trên YouTube trong Android?

Điều này có khả thi không? Nếu vậy, làm thế nào?

+0

Bạn đã cố gắng để bắt đầu một mình? – simchona

+0

Làm thế nào nó không hoạt động? SO sẽ không chỉ cung cấp cho bạn câu trả lời cho các vấn đề không xác định. – simchona

+0

Tôi muốn biết là nó có thể hay không? – Krishna

Trả lời

45

YouTube đặt hình thu nhỏ của video tại URL có thể dự đoán cụ thể. Nó sẽ là một chút đau, nhưng tôi chắc chắn bạn có thể tìm thấy một cách để hiển thị hình ảnh từ URL, hoặc để tải chúng và sau đó hiển thị chúng.

Dưới đây là thông tin trên blog của tôi về những gì các URL thu nhỏ là: http://alamoxie.com/blog/web-design/add-youtube-thumbnails/

tôi sẽ sao chép và dán những gì tôi đã viết trong bài viết trên blog:

Nhìn vào liên kết cho ví dụ bằng video cho , http://www.youtube.com/watch?v=GDFUdMvacI0

Lấy video ID… phần sau “v=”, trong trường hợp này là GDFUdMvacI0. Nếu URL dài hơn, chỉ đi đến ký hiệu tiếp theo. Ví dụ: http://www.youtube.com/watch?v=GDFUdMvacI0&feature=youtu.be giống nhau, GDFUdMvacI0.

Sau đó chỉ cần thay thế ID video của bạn cho ID video trong các URL sau để những hình ảnh thumbnail:

0.jpg là một hình ảnh có kích thước đầy đủ. Ba thiết bị còn lại rất nhỏ (120 × 90) và được YouTube lấy tự động từ ba điểm nhất định trong video.

+0

làm cách nào để có được id video theo chương trình? –

+0

Điều đó tùy thuộc vào thông tin bạn đang bắt đầu. Tôi không biết gì về kịch bản cụ thể của bạn, vì vậy tôi sợ tôi không thể giúp bạn ở đó. Vui lòng sửa đổi câu hỏi của bạn để bao gồm thêm chi tiết về những gì bạn đang làm - bạn có đang cố gắng phân tích cú pháp ID video ra khỏi URL không? Bạn có đang gửi video YouTube mới thông qua API không? Hay cái gì? –

+1

liên kết blog đã hết hạn, vui lòng cập nhật nó. –

2

Điều này có thể giúp ai đó. Ý tưởng đầu tiên là tải video bạn muốn, Ở đây tôi đã truy xuất danh sách video từ danh sách phát. Sau đó tôi đã sử dụng lớp này:
http://blog.blundell-apps.com/imageview-with-loading-spinner/
Để hiển thị thanh tiến trình trong khi hình thu nhỏ được truy xuất từ ​​web.

/*** 
* Fetch all videos in a playlist 
* @param playlistId 
* @return 
* @throws ClientProtocolException 
* @throws IOException 
* @throws JSONException 
*/ 
public YouTubePlaylist fetchPlaylistVideos(String playlistId) throws ClientProtocolException, IOException, JSONException { 
    String playlistUrl = "https://gdata.youtube.com/feeds/api/playlists/" + playlistId + "?v=2&alt=jsonc"; 
    HttpClient client = new DefaultHttpClient(); 
    HttpUriRequest request = new HttpGet(playlistUrl); 
    HttpResponse response = client.execute(request); 
    String jsonString = GeneralHelpers.convertToString(response.getEntity().getContent()); 
    JSONObject json = new JSONObject(jsonString); 

    if (jsonString.contains("Playlist not found")) { 
     Log.e(TAG, "playlist not found. id: " + playlistId); 
     return null; 
    } 

    JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items"); 

    String playlistTitle = json.getJSONObject("data").getString("title"); 
    String author = json.getJSONObject("data").getString("author"); 

    List<YouTubeVideo> videos = new ArrayList<YouTubeVideo>(); 
    for (int i = 0; i < jsonArray.length(); i++) { 
     JSONObject video = jsonArray.getJSONObject(i).getJSONObject("video"); 
     // The title of the video 
     String title = video.getString("title"); 

     String url; 
     try { 
      url = video.getJSONObject("player").getString("mobile"); 
     } catch (JSONException ignore) { 
      url = video.getJSONObject("player").getString("default"); 
     } 

     String thumbUrl = video.getJSONObject("thumbnail").getString("sqDefault"); 
     String videoId = video.getString("id"); 
     String uploaded = video.getString("uploaded"); 
     String duration = video.getString("duration"); 
     String minutes = (Integer.parseInt(duration)/60 < 10) ? "0" + (Integer.parseInt(duration)/60) : "" + (Integer.parseInt(duration)/60); 
     String seconds = (Integer.parseInt(duration) % 60 < 10) ? "0" + (Integer.parseInt(duration) % 60): "" + (Integer.parseInt(duration) % 60); 
     duration = minutes + ":" + seconds; 

     videos.add(new YouTubeVideo(title, author, url, thumbUrl, videoId, uploaded, duration)); 
    } 

    YouTubePlaylist playlist = new YouTubePlaylist(author, playlistId, playlistTitle, videos); 
    return playlist; 
}//end fetchPlaylistVideos 
21
  • Download file picasso jar và đặt rằng file jar trong "libs" thư mục

  • Sử dụng picasso tải hình ảnh

  • Sử dụng phương pháp extractYoutubeId(url) để trích xuất youtube id từ YoutubeVideo Url

Để tải hình ảnh của video youtube, hãy sử dụng liên kết n và đưa youtube id trong url như sau: "http://img.youtube.com/vi/"+extractYoutubeId(url)+"/0.jpg"

Youtube_Video_thumnail

package com.app.download_video_demo; 

    import java.net.MalformedURLException; 
    import java.net.URL; 

    import android.app.Activity; 
    import android.os.Bundle; 
    import android.util.Log; 
    import android.widget.ImageView; 

    import com.squareup.picasso.Picasso; 


    // get Picasso jar file and put that jar file in libs folder 

    public class Youtube_Video_thumnail extends Activity 
    { 
     ImageView iv_youtube_thumnail,iv_play; 
     String videoId; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      // TODO Auto-generated method stub 
      super.onCreate(savedInstanceState); 
      super.setContentView(R.layout.youtube_video_activity); 

      init(); 

      try 
      { 
       videoId=extractYoutubeId("http://www.youtube.com/watch?v=t7UxjpUaL3Y"); 

       Log.e("VideoId is->","" + videoId); 

       String img_url="http://img.youtube.com/vi/"+videoId+"/0.jpg"; // this is link which will give u thumnail image of that video 

       // picasso jar file download image for u and set image in imagview 

       Picasso.with(Youtube_Video_thumnail.this) 
       .load(img_url) 
       .placeholder(R.drawable.ic_launcher) 
       .into(iv_youtube_thumnail); 

      } 
      catch (MalformedURLException e) 
      { 
       e.printStackTrace(); 
      } 

     } 
     public void init() 
     { 
      iv_youtube_thumnail=(ImageView)findViewById(R.id.img_thumnail); 
      iv_play=(ImageView)findViewById(R.id.iv_play_pause); 
     } 

     // extract youtube video id and return that id 
     // ex--> "http://www.youtube.com/watch?v=t7UxjpUaL3Y" 
     // videoid is-->t7UxjpUaL3Y 


     public String extractYoutubeId(String url) throws MalformedURLException { 
      String query = new URL(url).getQuery(); 
      String[] param = query.split("&"); 
      String id = null; 
      for (String row : param) { 
       String[] param1 = row.split("="); 
       if (param1[0].equals("v")) { 
        id = param1[1]; 
       } 
      } 
      return id; 
     } 

    } 

youtube_video_activity.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <RelativeLayout 
     android:id="@+id/webvideo_layout2" 
     android:layout_width="250dp" 
     android:layout_height="180dp" 
     android:layout_gravity="center" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="10dp" 
     > 


     <ImageView 
      android:id="@+id/img_thumnail" 
      android:layout_width="250dp" 
      android:layout_height="180dp" 
      android:layout_centerInParent="true" 
      android:scaleType="fitXY" /> 

     <ImageView 
      android:id="@+id/iv_play_pause" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_centerInParent="true" 
      android:src="@drawable/icn_play" /> 
    </RelativeLayout> 

</LinearLayout> 
+1

công việc tuyệt vời ..... cảm ơn vì sự giúp đỡ .. – Bunny

+0

Làm việc tuyệt vời với Volley quá – BlackPearl

4

Hãy thử điều này

public static String getYoutubeThumbnailUrlFromVideoUrl(String videoUrl) { 
    String imgUrl = "http://img.youtube.com/vi/"+getYoutubeVideoIdFromUrl(videoUrl) + "/0.jpg"; 
    return imgUrl; 
} 

public static String getYoutubeVideoIdFromUrl(String inUrl) { 
    if (inUrl.toLowerCase().contains("youtu.be")) { 
     return inUrl.substring(inUrl.lastIndexOf("/") + 1); 
    } 
    String pattern = "(?<=watch\\?v=|/videos/|embed\\/)[^#\\&\\?]*"; 
    Pattern compiledPattern = Pattern.compile(pattern); 
    Matcher matcher = compiledPattern.matcher(inUrl); 
    if (matcher.find()) { 
     return matcher.group(); 
    } 
    return null; 
} 
+1

Giải pháp tốt nhất, hoạt động theo thực tế trong mọi trường hợp! Cảm ơn – Pelanes

Các vấn đề liên quan