如何运行的YouTube视频在Android模拟器 [英] How to run YouTube Video in Android Emulator

查看:759
本文介绍了如何运行的YouTube视频在Android模拟器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已在对此我获取的列表视图中的YouTube视频列表中的程序,并已实施的onClick还

来源: - 我按照有关如何使用YouTube gdata的教程。填充与从YouTube和视频的onclick列表视图。该人士$ ​​C $ C可以用:

的http://博客.blundell-apps.com /单击项-IN-A-列表视图到节目-的YouTube视频/

问题:

每当我点击任何YouTube视频项目行,让特定的视频在接下来的活动,但每当我点击视频来运行它不工作,每次只得到黑色空间

GetYouTubeUserVideosTask.java

 公共类GetYouTubeUserVideosTask实现Runnable { 公共静态最后的字符串库=库; 私人最终处理程序的replyTo; 私人最终字符串用户名;
    公共GetYouTubeUserVideosTask(处理器的replyTo,字符串username){
this.replyTo =的replyTo;
this.username =用户名;
  }  @覆盖
  公共无效的run(){
尝试{    HttpClient的客户端=新DefaultHttpClient();    HttpUriRequest要求=新HTTPGET
           (http://gdata.youtube.com/feeds/api/users/
            Google代码/上传V = 2及?ALT = jsonc);
    //获取响应了YouTube发回
    HTT presponse响应= client.execute(请求);
    //转换这种反应成可读的字符串
    字符串jsonString = StreamUtils.convertToString
            (response.getEntity()的getContent());
    //创建我们可以从字符串使用JSON对象
    JSONObject的JSON =新的JSONObject(jsonString);    //获取的搜索结果条目
    JSONArray jsonArray = json.getJSONObject(数据)getJSONArray(项目);    //创建一个列表存储在视频
    清单<视频>视频=新的ArrayList<视频>();    的for(int i = 0; I< jsonArray.length();我++){
        的JSONObject的JSONObject = jsonArray.getJSONObject(I)
        //视频的标题
        字符串title = jsonObject.getString(标题);
        // URL链接到YouTube,这会检查是否有一个移动网址
        //如果它不它得到的标准网址
        字符串URL;
        尝试{
    URL = jsonObject.getJSONObject(玩家)的getString(默认)。
        }赶上(JSONException忽略){
    URL = jsonObject.getJSONObject(玩家)的getString(默认)。
        }    字符串thumbUrl = jsonObject.getJSONObject
            (缩略图)的getString(sqDefault);        //创建视频对象,并把它添加到我们的名单
        videos.add(新视频(标题,URL,thumbUrl));
    }
    //创建库来保存我们的视频
    库lib =新库(用户名,视频);
    //包库到束发回活动
    捆绑数据=新包();
    data.putSerializable(库,LIB文件);    //发送数据包(我们的图书馆)回处理器(我们的活动)
    消息味精= Message.obtain();
    msg.setData(数据);
    replyTo.sendMessage(MSG);//我们没有做任何错误捕获,就什么都不会发生,如果这个任务落在了
}赶上(ClientProtocolException E){
    Log.e(Feck,E);
}赶上(IOException异常五){
    Log.e(Feck,E);
}赶上(JSONException E){
    Log.e(Feck,E);
}
    }

VideosListView.java

 公共类VideosListView扩展
  ListView控件实现android.widget.AdapterView.OnItemClickListener {私人列表<视频>视频;
私人VideoClickListener videoClickListener;公共VideosListView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
}公共VideosListView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
}公共VideosListView(上下文的背景下){
    超级(上下文);
}公共无效setVideos(列表<视频>视频){
    this.videos =视频;
    VideosAdapter适配器=新VideosAdapter(的getContext(),视频);
    setAdapter(适配器);
    //当视频设置,我们还成立一个项目点击监听到列表
    //这将回调,每当一个项目是pressed我们的自定义列表
    //它会告诉我们什么位置,在列表中pressed
    setOnItemClickListener(本);
}//调用此方法设置一个监听器列表
//不管类被传递的时候,列表是pressed将被通报
//(即在刚刚过去的类必须实施VideoClickListener
//意思是有可用的方法,我们要调用)
公共无效setOnVideoClickListener(VideoClickListener L){
    videoClickListener = 1;
}@覆盖
公共无效setAdapter(ListAdapter适配器){
    super.setAdapter(适配器);
}//当我们收到通知,一个列表项是否为pressed
//我们检查,看是否有视频监听器已设置@覆盖
公共无效onItemClick(适配器视图<>适配器,视图V,INT位置,长的id){
    如果(videoClickListener!= NULL){
        videoClickListener.onVideoClicked(videos.get(位置));
    }
}

VideoClickListener.java

 公共接口VideoClickListener {公共无效onVideoClicked(视频短片);  }


解决方案

模拟器不会播放YouTube视频,因为有不同的不同格式的YouTube,模拟器只支持3GP的视频,您可以在移动测试它,它会正常工作。

I have made a program in which i am fetching list of youtube videos in listview and i have implemented onClick also

source:- I have followed a tutorial on how to use the youtube gdata. Populating a listview with videos from youtube and a onclick. The source code is available on:

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

Problem:

Whenever i click on any of the youtube video item row, getting specific video in next activity but whenever i click on video to run it is not working everytime getting only black space

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);

  }

解决方案

Emulator wont play youtube videos, because youtube having different different formats, emulator supports only 3gp videos, you can test it in mobile it will work fine.

这篇关于如何运行的YouTube视频在Android模拟器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆