Created
September 7, 2018 13:58
-
-
Save gajerarajnit/bd10a9955ca1163d35b315501fbe1f78 to your computer and use it in GitHub Desktop.
Integrate custom youtube player using library.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical" | |
tools:context="com.rajnit.photostudio.YoutubeVideoActivity"> | |
<com.pierfrancescosoffritti.androidyoutubeplayer.player.YouTubePlayerView | |
android:id="@+id/youtube_player_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:visibility="visible"/> | |
<android.support.v4.view.ViewPager | |
android:id="@+id/viewPager" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" /> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:8.0.1' | |
implementation "android.arch.lifecycle:extensions:1.1.1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:orientation="vertical"> | |
<com.pierfrancescosoffritti.androidyoutubeplayer.player.YouTubePlayerView | |
android:id="@+id/youtube_player_view" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"/> | |
</LinearLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rajnit.photostudio.adapter; | |
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.support.v4.view.PagerAdapter; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.pierfrancescosoffritti.androidyoutubeplayer.player.YouTubePlayer; | |
import com.pierfrancescosoffritti.androidyoutubeplayer.player.YouTubePlayerView; | |
import com.pierfrancescosoffritti.androidyoutubeplayer.player.listeners.AbstractYouTubePlayerListener; | |
import com.pierfrancescosoffritti.androidyoutubeplayer.player.listeners.YouTubePlayerInitListener; | |
import com.pierfrancescosoffritti.androidyoutubeplayer.ui.PlayerUIController; | |
import com.rajnit.photostudio.R; | |
/** | |
* Created by PhotoStudio on 07/09/18. | |
*/ | |
public class VideoPagerAdapter extends PagerAdapter { | |
private Context mContext; | |
public VideoPagerAdapter(Context context) { | |
mContext = context; | |
} | |
@Override | |
public Object instantiateItem(ViewGroup collection, int position) { | |
LayoutInflater inflater = LayoutInflater.from(mContext); | |
ViewGroup layout = (ViewGroup) inflater.inflate(R.layout.item_youtube_video, collection, false); | |
collection.addView(layout); | |
YouTubePlayerView youtubePlayerView = layout.findViewById(R.id.youtube_player_view); | |
PlayerUIController playerUIController = youtubePlayerView.getPlayerUIController(); | |
playerUIController.showFullscreenButton(false); | |
youtubePlayerView.initialize(new YouTubePlayerInitListener() { | |
@Override | |
public void onInitSuccess(@NonNull final YouTubePlayer initializedYouTubePlayer) { | |
// youTubePlayer = initializedYouTubePlayer; | |
initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() { | |
@Override | |
public void onReady() { | |
String videoId = "-m3V8w_7vhk"; | |
initializedYouTubePlayer.cueVideo(videoId, 0); | |
} | |
}); | |
} | |
}, false); | |
return layout; | |
} | |
@Override | |
public void destroyItem(ViewGroup collection, int position, Object view) { | |
collection.removeView((View) view); | |
} | |
@Override | |
public int getCount() { | |
return 10; | |
} | |
@Override | |
public boolean isViewFromObject(View view, Object object) { | |
return view == object; | |
} | |
@Override | |
public CharSequence getPageTitle(int position) { | |
return "Page : " + position; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.rajnit.photostudio; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.v4.view.ViewPager; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.view.View; | |
import com.pierfrancescosoffritti.androidyoutubeplayer.player.YouTubePlayer; | |
import com.pierfrancescosoffritti.androidyoutubeplayer.player.YouTubePlayerView; | |
import com.pierfrancescosoffritti.androidyoutubeplayer.player.listeners.AbstractYouTubePlayerListener; | |
import com.pierfrancescosoffritti.androidyoutubeplayer.player.listeners.YouTubePlayerInitListener; | |
public class YoutubeVideoActivity extends AppCompatActivity { | |
private static final String TAG = "YoutubeVideoActivity"; | |
public ViewPager viewPager; | |
public YouTubePlayerView youtubePlayerView; | |
public YouTubePlayer youTubePlayer; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_youtube_video); | |
youtubePlayerView = findViewById(R.id.youtube_player_view); | |
// add below line and no need to handle pause or release youtube player... | |
getLifecycle().addObserver(youtubePlayerView); | |
youtubePlayerView.initialize(new YouTubePlayerInitListener() { | |
@Override | |
public void onInitSuccess(@NonNull final YouTubePlayer initializedYouTubePlayer) { | |
youTubePlayer = initializedYouTubePlayer; | |
initializedYouTubePlayer.addListener(new AbstractYouTubePlayerListener() { | |
@Override | |
public void onReady() { | |
String videoId = "-m3V8w_7vhk"; | |
initializedYouTubePlayer.loadVideo(videoId, 0); | |
} | |
}); | |
} | |
}, false); | |
youtubePlayerView.getPlayerUIController().setFullScreenButtonClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
Log.d(TAG, "setFullScreenButtonClickListener: "); | |
} | |
}); | |
youtubePlayerView.getPlayerUIController().showYouTubeButton(false); | |
youtubePlayerView.getPlayerUIController().showCurrentTime(false); | |
youtubePlayerView.getPlayerUIController().showDuration(false); | |
// youtubePlayerView.getPlayerUIController().showSeekBar(false); | |
/* Try viewpager demo... */ | |
viewPager = findViewById(R.id.viewPager); | |
viewPager.setAdapter(new VideoPagerAdapter(this)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment