Question

I found a very useful tutorial on how to create a listview of youtube videos:

http://blog.blundell-apps.com/show-youtube-user-videos-in-a-listview/

Then I followed another tutorial to make the videos in the listview clickable:

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

My problem is I do not want to play the videos with the youtube app - I would like to play them within MY app - so I created an instance of a youtube player:

http://android-er.blogspot.com/2013/06/simple-example-using-youtube-android.html

My question is how can I modify my current onClick listener to pass the video's ID to my app's internal player instead of the youtube app during my onClick event?

onClick Listener (currently passing to standalone YouTube app):


    @Override
    public void onVideoClicked(Video video) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(video.getUrl()));
        startActivity(intent);
    }

My Internal Youtube Player:

public class MainActivity extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener{

 public static final String API_KEY = "AIzaSyCe6tORd9Ch4lx-9Ku5SQ476uS9OtZYsWA";
 public static final String VIDEO_ID = "o7VVHhK9zf0";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        YouTubePlayerView youTubePlayerView = (YouTubePlayerView)findViewById(R.id.youtubeplayerview);
        youTubePlayerView.initialize(API_KEY, this);
    }

 @Override
 public void onInitializationFailure(Provider provider,
   YouTubeInitializationResult result) {
  Toast.makeText(getApplicationContext(), 
    "onInitializationFailure()", 
    Toast.LENGTH_LONG).show();
 }

 @Override
 public void onInitializationSuccess(Provider provider, YouTubePlayer player,
   boolean wasRestored) {
  if (!wasRestored) {
        player.cueVideo(VIDEO_ID);
      }
 }

}
Was it helpful?

Solution

Something like:

@Override
public void onVideoClicked(Video video) {
    Intent intent = new Intent(this, MainActivity.class); // Your youtubeplayer activity
    intent.putExtra("video_id",video_id);
    startActivity(intent);
}

And in MainActivity onCreate() you get the id and pass it to your Youtube player:

getIntent().getStringExtra("video_id");
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top