大家好。请尝试从youtube频道播放视频。让我来解释一下1:在第一个活动中,你在listview中得到一个用户列表2:当你点击用户,它加载他们的视频在另一个列表视图。到目前为止,一切都很完美,但我不知道如何播放视频,当那个特定的视频被点击时。这是我的代码。
package com.talagbe.videofeeds;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
public class YoutubeVideos extends Activity {
ListView listv;
YoutubeAdapter yadapter;
ArrayList<Youtube> y_list;
String url;
String Channel;
Context context=null;
ProgressDialog mprogress;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.youtube);
y_list= new ArrayList<Youtube>();
listv = (ListView) findViewById(R.id.list2);
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
Channel = bundle.getString("Channel");
init();
}
public void init(){
Log.d("Chan",Channel);
AsyncHttpClient LoadVideos = new AsyncHttpClient();
LoadVideos.get("https://gdata.youtube.com/feeds/api/users/"+ Channel +"/uploads?v=2&alt=jsonc", new AsyncHttpResponseHandler(){
public void onSuccess(String data){
try {
JSONObject videoObj = new JSONObject(data);
//JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
JSONArray videosarray = videoObj.getJSONObject("data").getJSONArray("items");;
for(int i=0; i<videosarray.length();i++){
Youtube yvideos = new Youtube();
JSONObject video = videosarray.getJSONObject(i);
String videourl = video.getJSONObject("player").getString("mobile");
yvideos.setVideoTitle(video.getString("title"));
yvideos.setThumbs(video.getJSONObject("thumbnail").getString("sqDefault"));
//yvideos.setVideourl(videourl);
y_list.add(yvideos);
Log.d("Title",video.getString("title"));
Log.d("Video",videourl);
Log.d("Thumb",video.getJSONObject("thumbnail").getString("sqDefault"));
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
YoutubeAdapter Adapter = new YoutubeAdapter(getApplicationContext(),R.layout.videos,y_list);
listv.setAdapter(Adapter);
}
public void onStart(){
mprogress = ProgressDialog.show(YoutubeVideos.this, "Connecting...", "Retrieveing Videos");
}
public void onFinish(){
mprogress.dismiss();
}
public void onFailure(Throwable error, String content)
{
//Log.e("Error", content);
Toast.makeText(getBaseContext(), "Error Connecting to the internet", Toast.LENGTH_LONG).show();
}
});
}
}
你可以使用Google提供的"YouTube Android Player API"来播放YouTube视频。
YouTube Android Player API允许您合并视频回放功能到你的Android应用程序。API定义了加载和播放YouTube视频(和播放列表)的方法自定义和控制视频播放体验。
下面是一个使用YouTube Android Player API的示例应用。
您可以关注YouTube APIhttps://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubeIntents或者你可以直接调用intent默认的YouTube播放器:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + id));
startActivity(intent);
id是url中问号后面的标识符。例如:youtube.com/watch?v=ID
Intent videoIntent = new Intent(Intent.ACTION_VIEW);
videoIntent.setData(url);
videoIntent.setClassName("com.google.anddroid.youtube", "com.google.android.youtube.WatchActivity");
startActivity(videoIntent);