在我的Android活动中,我想通过YouTube应用程序或其他方式从YouTube播放视频。为此,我想在我的活动中加载视频的缩略图。
这可能吗?如果是这样,怎么样?
答案 0 :(得分:58)
YouTube将视频的缩略图放在特定的可预测网址上。这会有点痛苦,但我确信您可以找到一种方法来显示URL中的图像,或者下载它们然后显示它们。
以下是我的博客上有关这些缩略图网址的信息: http://alamoxie.com/blog/web-design/add-youtube-thumbnails/
我将复制并粘贴我在博文中写的内容:
查看视频的链接,例如http://www.youtube.com/watch?v=GDFUdMvacI0
取video ID
... “v=”
之后的部分,在本例中为GDFUdMvacI0
。如果URL长于此值,则只能执行下一个&符号。例如,http://www.youtube.com/watch?v=GDFUdMvacI0&feature=youtu.be
是相同的,GDFUdMvacI0
。
然后,只需将您的视频ID替换为以下网址中的视频ID即可:
0.jpg
是一张全尺寸图片。其他三个非常小(120×90),并由YouTube从视频中的三个特定点自动拍摄。
答案 1 :(得分:23)
下载picasso jar文件并将该jar文件放入" libs"夹
使用picasso下载图片
使用方法extractYoutubeId(url)
从YoutubeVideo网址中提取youtube ID
要获取youtube视频的图片,请使用指定的链接,并将youtube id放在该网址中,如下所示:"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>
答案 2 :(得分:7)
试试这个
public static String getYoutubeThumbnailUrlFromVideoUrl(String videoUrl) {
return "http://img.youtube.com/vi/"+getYoutubeVideoIdFromUrl(videoUrl) + "/0.jpg";
}
public static String getYoutubeVideoIdFromUrl(String inUrl) {
inUrl = inUrl.replace("&feature=youtu.be", "");
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;
}
答案 3 :(得分:2)
这可能对某人有所帮助。我们的想法是首先获取您想要的视频,我在这里检索了播放列表中的视频列表。之后我用了这个课:
http://blog.blundell-apps.com/imageview-with-loading-spinner/
在从网络中检索缩略图时显示进度条。
/***
* 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