Reputation: 851
Good day everyone. Please am trying to play videos from a youtube channel. Let me break it down 1:On the first activity you get a list of users in a listview 2:When you click on the user,it loads their videos in another listview. Everything is working perfectly to this point,But i don't know how to play the video,when that particular video is clicked. Here is my code.
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();
}
});
}
}
Upvotes: 0
Views: 1016
Reputation: 1
You can follow YouTube API https://developers.google.com/youtube/android/player/reference/com/google/android/youtube/player/YouTubeIntents or you can direct intent default YouTube player by calling :
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube://" + id));
startActivity(intent);
The id is the identifier after the questionmark in the url. For example: youtube.com/watch?v=ID
Another way is:
Intent videoIntent = new Intent(Intent.ACTION_VIEW);
videoIntent.setData(url);
videoIntent.setClassName("com.google.anddroid.youtube", "com.google.android.youtube.WatchActivity");
startActivity(videoIntent);
Upvotes: 0
Reputation: 12877
Here's a sample app that utilizes YouTube Android Player API.
Upvotes: 1
Reputation: 5365
You can make use of "YouTube Android Player API" provided by Google to play YouTube videos.
The YouTube Android Player API enables you to incorporate video playback functionality into your Android applications. The API defines methods for loading and playing YouTube videos (and playlists) and for customizing and controlling the video playback experience.
Upvotes: 1