如何在Android模拟器中运行YouTube视频

时间:2012-12-19 07:09:44

标签: android android-emulator youtube youtube-api

我制作了一个程序,我在listview中获取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);

  }

2 个答案:

答案 0 :(得分:7)

仿真器不会播放youtube视频,因为youtube有不同的格式,模拟器只支持3gp视频,你可以在手机上测试它会正常工作。

答案 1 :(得分: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;
             }
             }
         });
     }

 }