如何从搜索意图中选择 YouTube 链接 [英] How to choosed youtube link from search intent
本文介绍了如何从搜索意图中选择 YouTube 链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想从 youtube 搜索中选择视频链接.
i want to get picked video link from youtube search.
我使用此意图发送到 youtube 上的用户搜索:
I use this intent to send to user search on youtube:
Intent intent = new Intent(Intent.ACTION_SEARCH);
intent.setPackage("com.google.android.youtube");
intent.putExtra(searchYouTube.getText().toString(), "Android");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, YOUTUBE);
我的问题是我应该在 onActivityResult 中放什么?
My problem is what should i put in my onActivityResult?
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == YOUTUBE && resultCode == Activity.RESULT_OK)
..
..
* get the choosed video link *
..
感谢您的帮助.
推荐答案
我使用 youtube api 解决了这个问题.下载 youtube api.这是我制作的示例代码,用于获取 youtube 视频并将结果与视频图像一起显示.
I solved it by using youtube api. Download the youtube api. Here is a sample code i made to get youtube viedos and display the result with the video image.
public void getVideos() {
youtubeSearch = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY,
new HttpRequestInitializer() {
public void initialize(HttpRequest request)
throws IOException {
}
}).setApplicationName("youtube-cmdline-search-sample").build();
YouTube.Search.List search = null;
try {
search = youtubeSearch.search().list("id,snippet");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
search.setKey("YOUT_KEY");
search.setQ(searchYouTube.getText().toString());
search.setType("video");
search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
SearchListResponse searchResponse = null;
try {
searchResponse = search.execute();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
searchResultList = searchResponse.getItems();
iteratorSearchResults = searchResultList.iterator();
if (!iteratorSearchResults.hasNext()) {
Toast.makeText(this, mResources.getString(R.string.There_arent_any_results_for_your_query),
Toast.LENGTH_SHORT).show();
return;
}
int counter = 0;
int length = searchResultList.size();
titles = new String[length];
ids = new String[length];
images = new String[length];
urls = new String[length];
while (iteratorSearchResults.hasNext()) {
SearchResult singleVideo = iteratorSearchResults.next();
ResourceId rId = singleVideo.getId();
if (rId.getKind().equals("youtube#video")) {
String urlString = "https://i.ytimg.com/vi/" +rId.getVideoId() + "/0.jpg";
URL url = null;
try {
url = new URL(urlString);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String picture = null;
try {
picture = encodeTobase64(getResizedBitmap(BitmapFactory.decodeStream(url.openConnection().getInputStream()), 100, 100));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
titles[counter] = singleVideo.getSnippet().getTitle();
ids[counter] = rId.getVideoId();
images[counter] = picture;
urls[counter] = urlString;
counter++;
}
}
inflater = (LayoutInflater) DatePage.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ArrayAdapter<String> parametersAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, titles) {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
if(convertView==null)
vi = inflater.inflate(R.layout.youtube_list, null);
TextView text = (TextView) vi.findViewById(R.id.youtubeText);
ImageView image = (ImageView) vi.findViewById(R.id.youtubeImage);
text.setText(titles[position]);
image.setImageDrawable(new BitmapDrawable(getResources(),decodeBase64(images[position])));
return vi;
}
};
builderYoutube = new AlertDialog.Builder(this);
builderYoutube.setSingleChoiceItems(parametersAdapter, -1, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
youtube = ids[which];
thumbnailLink = urls[which];
youtubeTitle = titles[which];
if(youtubeFromText.isChecked())
youtubeFromText.setChecked(false);
youtubeFromSearch.setChecked(true);
Toast.makeText(DatePage.this, mResources.getString(R.string.Picked) + " " + titles[which], Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
这篇关于如何从搜索意图中选择 YouTube 链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文