2

リストビューでYouTubeビデオのリストを取得するプログラムを作成し、onClickも実装しました

ソース:- YouTube gdata の使用方法に関するチュートリアルに従いました。リストビューに YouTube と onclick の動画を取り込みます。ソースコードは次で入手できます。

http://blog.blundell-apps.com/click-item-in-a-listview-to-show-youtube-video/

問題:

YouTube ビデオ項目の行のいずれかをクリックすると、次のアクティビティで特定のビデオが取得されますが、ビデオをクリックして実行するたびに、黒いスペースのみが取得されるたびに機能しません。

GetYouTubeUserVideosTask.java

 public class GetYouTubeUserVideosTask implements Runnable {

 public static final String LIBRARY = "Library";

 private final Handler replyTo;

 private final String username;


    public GetYouTubeUserVideosTask(Handler replyTo, String username) {
this.replyTo = replyTo;
this.username = username;
  }

  @Override
  public void run() {
try {

    HttpClient client = new DefaultHttpClient();

    HttpUriRequest request = new HttpGet
           ("http://gdata.youtube.com/feeds/api/users/
            GoogleDevelopers/uploads?v=2&alt=jsonc");
    // Get the response that YouTube sends back
    HttpResponse response = client.execute(request);
    // Convert this response into a readable string
    String jsonString = StreamUtils.convertToString
            (response.getEntity().getContent());
    // Create a JSON object that we can use from the String
    JSONObject json = new JSONObject(jsonString);

    // Get are search result items
    JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");

    // Create a list to store are videos in
    List<Video> videos = new ArrayList<Video>();

    for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);
        // The title of the video
        String title = jsonObject.getString("title");
        // The url link back to YouTube, this checks if it has a mobile url
        // if it doesnt it gets the standard url
        String url;
        try {
    url = jsonObject.getJSONObject("player").getString("default");
        } catch (JSONException ignore) {
    url = jsonObject.getJSONObject("player").getString("default");
        }

    String thumbUrl = jsonObject.getJSONObject
            ("thumbnail").getString("sqDefault");

        // Create the video object and add it to our list
        videos.add(new Video(title, url, thumbUrl));
    }
    // Create a library to hold our videos
    Library lib = new Library(username, videos);
    // Pack the Library into the bundle to send back to the Activity
    Bundle data = new Bundle();
    data.putSerializable(LIBRARY, lib);

    // Send the Bundle of data (our Library) back to the handler (our Activity)
    Message msg = Message.obtain();
    msg.setData(data);
    replyTo.sendMessage(msg);

// We don't do any error catching, just nothing will happen if this task falls over
} catch (ClientProtocolException e) {
    Log.e("Feck", e);
} catch (IOException e) {
    Log.e("Feck", e);
} catch (JSONException e) {
    Log.e("Feck", e);
}
    }

VideosListView.java

  public class VideosListView extends 
  ListView implements    android.widget.AdapterView.OnItemClickListener {

private List<Video> videos;
private VideoClickListener videoClickListener;

public VideosListView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
}

public VideosListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public VideosListView(Context context) {
    super(context);
}

public void setVideos(List<Video> videos){
    this.videos = videos;
    VideosAdapter adapter = new VideosAdapter(getContext(), videos);
    setAdapter(adapter);
    // When the videos are set we also set an item click listener to the list
    // this will callback to our custom list whenever an item it pressed
    // it will tell us what position in the list is pressed
    setOnItemClickListener(this);
}

// Calling this method sets a listener to the list
// Whatever class is passed in will be notified when the list is pressed
// (The class that is passed in just has to 'implement VideoClickListener'
// meaning is has the methods available we want to call)
public void setOnVideoClickListener(VideoClickListener l) {
    videoClickListener = l;
}

@Override
public void setAdapter(ListAdapter adapter) {
    super.setAdapter(adapter);
}

// When we receive a notification that a list item was pressed
// we check to see if a video listener has been set

@Override
public void onItemClick(AdapterView<?> adapter, View v, int position, long id) {
    if(videoClickListener != null){
        videoClickListener.onVideoClicked(videos.get(position));
    }
}

VideoClickListener.java

 public interface VideoClickListener {

public void onVideoClicked(Video video);

  }
4

2 に答える 2

7

エミュレータはYouTubeビデオを再生しません.YouTubeはさまざまなフォーマットを持っているため、エミュレータは3gpビデオのみをサポートしています.モバイルでテストすることができます.

于 2012-12-19T07:14:13.057 に答える
1

私はこのコードを試しました。これでYouTubeビデオを見ることができます。このコードを試して、あなたのステータスを教えてください。

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
     @ Override


     public void onCreate (Bundle savedInstanceState) {
         super. onCreate (savedInstanceState);



         setContentView (R.layout.activity_main);

         WebView web = (WebView) findViewById (R.id.webView1);
         web. getSettings().setJavaScriptEnabled (true);
         web. getSettings().setJavaScriptCanOpenWindowsAutomatically (false);
         web. getSettings().setPluginsEnabled (true);
         web. getSettings().setSupportMultipleWindows (false);
         web. getSettings().setSupportZoom (false);
         web. setVerticalScrollBarEnabled (false);
         web. setHorizontalScrollBarEnabled (false);


         web. loadUrl ("http://www.youtube.com/watch?v=Wg65ohhDldc");

         web. setWebViewClient (new WebViewClient () {
             @ Override public boolean shouldOverrideUrlLoading(WebView view, String url) {
             if (url.startsWith("vnd.youtube")){

             startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));

             return true;
             }
             else
             {
             return false;
             }
             }
         });
     }

 }
于 2013-03-25T07:19:56.837 に答える