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

标签 android android-emulator youtube youtube-api

我制作了一个程序,我在其中获取 ListView 中的 youtube 视频列表,并且我还实现了 onClick

来源:- 我遵循了有关如何使用 youtube gdata 的教程。使用来自 youtube 的视频和 onclick 填充 ListView 。源代码位于:

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

问题:

每当我点击任何 youtube 视频项目行时,都会在下一个 Activity 中获取特定视频,但每当我点击视频运行时,它都无法正常工作,每次都只获得黑色空间

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

  }

最佳答案

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

关于android - 如何在 Android 模拟器中运行 YouTube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13947223/

相关文章:

Android 模拟器 API21 未启动

java - 选中 CheckBox 时退出显示 Activity

android - 使用所有用户数据(包括谷歌帐户)克隆 Android 模拟器

android - ViewPager 从错误的地方开始?

java - 加速度计错误报告

iOS Youtube API v3 订阅 youtube channel

facebook - 在youtube上发布带有youtube视频的页面,直接在facebook中播放

javascript - 使用 Javascript 将类添加到 YouTube iframe 元素

Java SDK 管理器崩溃,命令行输出中出现 "flashplayerplugin"

android - 从 expo 连接时的 Windows 防火墙问题