0

我想从 youtube 搜索中选择视频链接。

我使用此意图发送给 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 中放什么?

protected void onActivityResult(int requestCode, int resultCode,
            Intent data) {

if (requestCode == YOUTUBE && resultCode == Activity.RESULT_OK)
..
..
* get the choosed video link *
..

感谢您的帮助。

4

1 回答 1

0

我通过使用 youtube api 解决了它。下载 youtube api。这是我为获取 youtube viedos 并使用视频图像显示结果而制作的示例代码。

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();
        }
    }); 
}
于 2013-10-31T19:08:57.370 回答