我已在對此我取的ListView中的YouTube視頻列表中的程序,並已實施的onClick也如何在Android模擬器上運行的YouTube視頻
來源: - 我已經按照如何使用教程youtube gdata。使用YouTube和YouTube上的視頻填充列表視圖。源代碼可以在:
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);
}
朋友你有試過嗎? – Udhikriman
是的,我試過只有3gp的視頻將工作,如果你在移動YouTube視頻測試也將工作 – Mohan
謝謝,我會嘗試然後讓你知道現在感謝 – Udhikriman