Getting Youtube videos using RecyclerView and cardView with Firebase in Android studio - android

I have been working on an android project in which I have used YouTube API and Firebase as a database, I also used recyclerView in it but my videos from Firebase database are not showing in the application, I have implemented it correctly I guess but there is something wrong with it, please help.
I am using Android Studio
YouTube Firebase Adapter java class:
#Override
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.youtube_videos);
YDatabaseReference = FirebaseDatabase.getInstance().getReference().child("youT");
YouRecycler = (RecyclerView)findViewById(R.id.Youtube_recycler);
}
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<post2youtube, YoutubeViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<post2youtube, YoutubeViewHolder>(
post2youtube.class,
R.layout.youtube_videos_card,
YoutubeViewHolder.class,
YDatabaseReference
) {
#Override
protected void populateViewHolder(YoutubeViewHolder viewHolder, post2youtube model, int position) {
viewHolder.setYoutube(model.getYoutubing());
}
};
YouRecycler.setAdapter(firebaseRecyclerAdapter);
}
public static class YoutubeViewHolder extends RecyclerViewPager.ViewHolder {
View yView;
public YoutubeViewHolder(View itemView) {
super(itemView);
yView = itemView;
}
public void setYoutube(final String youtubing){
final YouTubePlayerView youPlay = (YouTubePlayerView) yView.findViewById(R.id.youtuber);
youPlay.initialize("SOME KEY",
new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.cueVideo(youtubing);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult youTubeInitializationResult) {
}
});
}
Getter and setters class :
public class post2youtube {
private String youtubing;
public post2youtube(){
}
public post2youtube(String youtubing) {
this.youtubing = youtubing;
}
public String getYoutubing() {
return youtubing;
}
public void setYoutubing(String youtubing) {
this.youtubing = youtubing;
}
}
RecyclerView xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Youtube_recycler"/>
</LinearLayout>
CardView xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtuber"
android:layout_width="match_parent"
android:layout_height="200dp" />
</LinearLayout>
</android.support.v7.widget.CardView>

Related

Panorama Solution for a Camera Application

Is there a panorama open-source solution for the Android camera app?
I came across these options:
panoramagl-android
WideAnglePanoramaModule.java
but they look a little old and have no support, is there anything newer?
I also came across this page:
But there is not much documentation about it that I could find.
I would like to get a full solution including capturing the images, stitching and displaying.
Look at this article:
https://itnext.io/android-360-panorama-cedbfb47a9e
The thing that i dont like is that all images are from assets. I did it with image from firebase:
add dependancy:
dependencies {
/*
* Google VR SDK
* Release version: https://github.com/googlevr/gvr-android-sdk/releases
* */
implementation 'com.google.vr:sdk-panowidget:1.180.0'
implementation 'com.github.silvestrpredko:dot-progress-bar:1.1'
}
add class
public class SimpleVrPanoramaActivity extends Activity {
#BindView(R.id.progressBar2)
DotProgressBar dotProgressBar;
#BindView(R.id.pano_view)
VrPanoramaView panoWidgetView;
private String bar_panorama1;
private String mPost_key = null;
private Bitmap[] bitmaps = new Bitmap[1];
private DatabaseReference databaseReference;
private VrPanoramaView.Options panoOptions = new VrPanoramaView.Options();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_panorama);
ButterKnife.bind(this);
if (getIntent().getExtras() != null) {
mPost_key = getIntent().getExtras().getString("bar_id");
}
panoOptions.inputType = VrPanoramaView.Options.TYPE_MONO;
databaseReference = FirebaseDatabase.getInstance().getReference().child("bars").child(mPost_key).child("pano");
fetchPanorama();
}
private void fetchPanorama() {
databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
bar_panorama1 = dataSnapshot.child("i1").getValue(String.class);
glideImage(bar_panorama1);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
Toast.makeText(SimpleVrPanoramaActivity.this, "Проверьте подключение к интернету!", Toast.LENGTH_LONG).show();
}
});
}
public void glideImage(String bar_panorama) {
Glide.with(SimpleVrPanoramaActivity.this)
.asBitmap()
.load(bar_panorama)
.into(new CustomTarget<Bitmap>() {
#Override
public void onLoadFailed(#Nullable Drawable errorDrawable) {
dotProgressBar.setVisibility(View.GONE);
}
#Override
public void onResourceReady(#NonNull Bitmap resource, #Nullable Transition<? super Bitmap> transition) {
bitmaps[0] = resource;
panoWidgetView.loadImageFromBitmap(bitmaps[0], panoOptions);
dotProgressBar.setVisibility(View.GONE);
}
#Override
public void onLoadCleared(#Nullable Drawable placeholder) {
}
});
}
#Override
protected void onPause() {
panoWidgetView.pauseRendering();
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
panoWidgetView.resumeRendering();
}
#Override
protected void onDestroy() {
// Destroy the widget and free memory.
panoWidgetView.shutdown();
super.onDestroy();
}
}
add xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/colorPrimary"
tools:context=".aktau.panorama.PanoramaGLActivity">
<include
android:id="#+id/toolbar_pano"
layout="#layout/toolbar_pano"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<View
android:id="#+id/yellow_layer"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorOrange"
android:layout_below="#+id/toolbar_pano"
/>
<com.google.vr.sdk.widgets.pano.VrPanoramaView
android:id="#+id/pano_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/yellow_layer"/>
<com.github.silvestrpredko.dotprogressbar.DotProgressBar
android:id="#+id/progressBar2"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_below="#+id/toolbar_pano"
android:layout_alignParentBottom="true"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
app:amount="3"
android:layout_centerHorizontal="true"
app:animationDirection="right"
app:duration="#android:integer/config_shortAnimTime"
app:endColor="#color/colorPrimaryDark"
app:startColor="#color/colorLightBlue" />
</RelativeLayout>

YoutubePlayer is not playing video and giving error "Network error (400)

I am trying to load a YouTube's video and play it but it is showing network error with error code 400.I am using You tube player ( you can see in xml),I have already registered the app in developer console and followed instructions, but after loading sometimes, it is showing "An error occured" Don't know what i am missing.Any help is appreciated. Thanks
public class AboutDescriptionActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {
private YouTubePlayerView youTubeView;
Toolbar toolbar;
private static final int RECOVERY_DIALOG_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_about_description);
toolbar = (Toolbar) findViewById(R.id.aboutscreentoolbar);
toolbar.setTitle("About");
youTubeView = (YouTubePlayerView) findViewById(R.id.videoviewabout);
youTubeView.initialize(Config.DEVELOPER_KEY,this);
toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.backarrowicon));
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//What to do on back clicked
onBackPressed();
}
});
}
#Override
public void onBackPressed() {
finish();
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
// loadVideo() will auto play video
// Use cueVideo() method, if you don't want to play it automatically
player.cueVideo("https://youtu.be/CMvxh_uXEUc",1);
player.play();
// Hiding player controls
player.setPlayerStyle(YouTubePlayer.PlayerStyle.DEFAULT);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
getString(R.string.error_player), errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST){
getYouTubePlayerProvider().initialize(Config.DEVELOPER_KEY, this);
}
}
private YouTubePlayer.Provider getYouTubePlayerProvider() {
return (YouTubePlayerView) findViewById(R.id.videoviewabout);
}
}
And my XML file is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.simpalm.peacenow_android.AboutDescriptionActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/aboutscreentoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ToolbarStyle"
app:titleTextColor="#ffffff"/>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/aboutscreentoolbar"
android:fillViewport="false"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/abouttextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/aboutscreentoolbar"
android:layout_marginTop="20dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/Abouttext"
android:textSize="18sp"
android:textStyle="bold"/>
<com.google.android.youtube.player.YouTubePlayerView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:id="#+id/videoviewabout">
</com.google.android.youtube.player.YouTubePlayerView>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Instead of passing complete URL pass only YouTube VideoID
Instead of this
player.cueVideo("https://youtu.be/CMvxh_uXEUc",1);
Try this
player.cueVideo("CMvxh_uXEUc",1);

DataBinding: How to use BaseActivity / How to use Abstraction

I am trying to add DataBinding to my app. In my app, I have a BaseActivity which has a Toolbar and a FrameLayout. FrameLayout is container for activities' which extend the BaseActivity. How can I add databinding to both my BaseActivity and the extending activities?
I'll share my code without DataBinding:
Here is my BaseActivity.java:
public class BaseActivity extends AppCompatActivity {
#Override
public void setContentView(#LayoutRes int layoutResID) {
LinearLayout container = (LinearLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
FrameLayout activityContent = (FrameLayout) container.findViewById(R.id.activityContent);
getLayoutInflater().inflate(layoutResID, activityContent, true);
super.setContentView(container);
Toolbar toolbar = (Toolbar) container.findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(menuItem);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
}
Here is activity_base.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<include
android:id="#+id/toolbarLayout"
layout="#layout/toolbar" />
<FrameLayout
android:id="#+id/activityContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</layout>
Here is the activity which extends BaseActivity: CardRecyclerViewActivity.java:
public class CardRecyclerViewActivity extends BaseActivity {
RecyclerView recyclerView;
ReplikAdapter adapter;
ArrayList<Replik> replikListesi;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_card_recyclerview);
replikListesiniDoldur();
adapter = new ReplikAdapter(replikListesi, this, R.layout.item_card_replik);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
recyclerView.setAdapter(adapter);
setTitle(R.string.cardRecylerView);
}
public void replikListesiniDoldur() {
replikListesi = new ArrayList<Replik>();
replikListesi.add(new Replik(R.drawable.harvey1, "Ben ihtimallere oynamam. Adama oynarım.", "Harvey Specter"));
replikListesi.add(new Replik(R.drawable.harvey2, "Avukatlık doktorluğa çok benzer, acıtana kadar bastırırsın ve böylece nereye bakman gerektiğini anlarsın.", "Harvey Specter"));
replikListesi.add(new Replik(R.drawable.harvey3, "İşte aramızdaki fark bu; Sen küçük kaybetmek istiyorsun ben ise büyük kazanmak.", "Harvey Specter"));
replikListesi.add(new Replik(R.drawable.harvey4, "Benim hayallerim yok, hedeflerim var.", "Harvey Specter"));
}
}
Okey, I add DataBinding to my BaseActivity.java like that:
public class BaseActivity extends AppCompatActivity {
#Override
public void setContentView(#LayoutRes int layoutResID) {
ActivityBaseBinding binding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.activity_base, null, false);
getLayoutInflater().inflate(layoutResID, binding.activityContent, true);
super.setContentView(binding.getRoot());
setSupportActionBar(binding.toolbarLayout.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getItemId() == android.R.id.home) {
finish();
}
return super.onOptionsItemSelected(menuItem);
}
public void setTitle(String title) {
getSupportActionBar().setTitle(title);
}
}
activity_base.xml with DataBinding:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/toolbarLayout"
layout="#layout/toolbar" />
<FrameLayout
android:id="#+id/activityContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</layout>
But I am not sure how to add DataBinding to my extending activities in this scenerio. Could you help me out, please?
Might be late for this question.
Your BaseActivity should looks like following.
public abstract class BaseActivity<B extends ViewDataBinding, T extends BaseViewModel> extends AppCompatActivity {
protected B dataBinding;
protected T baseViewModel;
protected void bindView(int layoutId) {
dataBinding = DataBindingUtil.setContentView(this, layoutId);
}
#Override
protected void onDestroy() {
baseViewModel.detachView();
super.onDestroy();
}
}
Your MainActivity should looks like following.
public class MainActivity extends BaseActivity<ActivityMainBinding, MainActivityViewModel> implements MainActivityView, View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bindView(R.layout.activity_main);
baseViewModel = new MainActivityViewModel();
baseViewModel.attachView(this);
dataBinding.btnPerformOperation.setOnClickListener(this);
}
#Override
public void OnDataLoad(String item) {
dataBinding.setIsLoading(false);
startActivity(new Intent(MainActivity.this, SecondActivity.class).putExtra("result", item));
}
#Override
public void OnError(Throwable throwable) {
}
#Override
public void onClick(View v) {
dataBinding.setIsLoading(true);
baseViewModel.loadNextData();
}
}
Reference source code can be download from here.
Hope this helps you.
how about this?
BaseActivityViewModel.java
public class BaseActivityViewModel {
public void onClick() {
Log.i("BaseActivityViewModel | onClick", "111111111111");
}
}
activity_base.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="model"
type="kr.changhoonjin.textmvvmwithextend.BaseActivityViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="#{()->model.onClick()}"
android:text="base"/>
<FrameLayout
android:id="#+id/layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</layout>
BaseActivity.java
public class BaseActivity extends AppCompatActivity {
private ActivityBaseBinding binding;
#Override protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this, R.layout.activity_base);
binding.setModel(new BaseActivityViewModel());
}
protected <T extends ViewDataBinding> T putContentView(#LayoutRes int resId) {
return DataBindingUtil.inflate(getLayoutInflater(), resId, binding.layoutContainer, true);
}
}
MainActivityViewModel.java
public class MainActivityViewModel {
public void onClick2() {
Log.i("MainActivityViewModel | onClick2", "2222222222222");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="model"
type="kr.changhoonjin.textmvvmwithextend.MainActivityViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="#{()->model.onClick2()}"
android:text="main"/>
</LinearLayout>
</layout>
MainActivity.java
public class MainActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = putContentView(R.layout.activity_main);
binding.setModel(new MainActivityViewModel());
}
}
It will be same as you have done in BaseActivity, instead of inflate just use setContentView
private ActivityCardRecyclerviewBinding binding;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = DataBindingUtil.setContentView(this,R.layout.activity_card_recyclerview);
}

show Action bar in YouTube activity

I using youtube api and playing youtube video using below tag in xml
<?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"
tools:context="com.aspire.urdu.toddlers.poems.VideoListingActivity">
<include
android:id="#+id/toolbar"
layout="#layout/action_bar" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtubeplayerview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:hardwareAccelerated="true" />
</LinearLayout>
and i am using this code in activity.
public class VideoPlayerActivity extends YouTubeBaseActivity{
YouTubePlayer YTplayer;
YouTubePlayerView youTubePlayerView;
private String videoLink = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
videoLink = getIntent().getStringExtra("link");
youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(getResources().getString(R.string.you_tube_api_key), new YouTubePlayer.OnInitializedListener() {
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
YTplayer = youTubePlayer;
YTplayer.setShowFullscreenButton(false);
YTplayer.loadVideo(videoLink);
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(getApplicationContext(), ""+youTubeInitializationResult.name(), Toast.LENGTH_LONG).show();
}
});
}
}
but when video is playing it is showing on full screen..
What i need
i just need to display action with back button enabled Thanks.

YouTubePlayerFragment not working(Playing) in CollapsingToolbarLayout

YouTubePlayerFragment not working(Playing) in CollapsingToolbarLayout.
I want to play youtube video from youtube android api. Inside CollapsingToolbarLayout, YouTubePlayerFragment does not play, only thumbnail comes and after 1 seconds it goes to stop.
I it work fine inside other layout. I want material design collapsing layout with youtube video. Please help me.
<android.support.design.widget.CoordinatorLayout
android:id="#+id/root_coordinator"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<!--<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="#drawable/ic_launcher"
android:visibility="visible"
app:layout_collapseMode="parallax" />-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutYoutube"
android:layout_width="match_parent"
android:layout_height="250dp"
android:visibility="visible"
app:layout_collapseMode="parallax">
<fragment
android:id="#+id/youtube_fragment"
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/app_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:layout_collapseMode="pin">
<include
layout="#layout/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="1dp"
android:background="#android:color/white"
android:paddingTop="2dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
The official documentation says:
Note that while videos are playing, this View has a minimum size of 200x110 dp. If you make the view any smaller, videos will automatically stop playing. Also, it is not permitted to overlay this fragment's view with other views while a video is playing.
So there is no way to get to work YouTubePlayerFragment and
YouTubePlayerView in CollapsingToolbarLayout.
PS: If someone managed to do it - share the solution. I had this same problem, but I could not solve it.
It would have been helpful if you had posted the logic code or java code and the error log. I am posting the java code, please take a look at this one might work for you too...
public class YoutubeActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener{
public static final String API_KEY = "xxxxxxxxxx";
public static final String VIDEO_ID = "xxxxxxx";
private YouTubePlayer youTubePlayer;
private YouTubePlayerFragment youTubePlayerFragment;
private TextView textVideoLog;
private Button btnViewFullScreen;
private static final int RQS_ErrorDialog = 1;
private MyPlayerStateChangeListener myPlayerStateChangeListener;
private MyPlaybackEventListener myPlaybackEventListener;
String log = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_youtube);
youTubePlayerFragment = (YouTubePlayerFragment)getFragmentManager()
.findFragmentById(R.id.youtubeplayerfragment);
youTubePlayerFragment.initialize(API_KEY, this);
textVideoLog = (TextView)findViewById(R.id.videolog);
myPlayerStateChangeListener = new MyPlayerStateChangeListener();
myPlaybackEventListener = new MyPlaybackEventListener();
btnViewFullScreen = (Button)findViewById(R.id.btnviewfullscreen);
btnViewFullScreen.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View arg0) {
youTubePlayer.setFullscreen(true);
}});
Button btnPlay = (Button) findViewById(R.id.btnplay);
btnPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(youTubePlayer.isPlaying())
youTubePlayer.pause();
else
youTubePlayer.play();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_splash_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
Log.e("id", id+"");
switch(item.getItemId()){
case R.id.action_share:
break;
case 16908332:
onBackPressed();
break;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youtube, boolean b) {
youTubePlayer = youtube;
Toast.makeText(getApplicationContext(), "YouTubePlayer.onInitializationSuccess()", Toast.LENGTH_LONG).show();
youtube.setPlayerStateChangeListener(myPlayerStateChangeListener);
youtube.setPlaybackEventListener(myPlaybackEventListener);
if (!b) {
youTubePlayer.cueVideo(VIDEO_ID);
}
}
#Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if (youTubeInitializationResult.isUserRecoverableError()) {
youTubeInitializationResult.getErrorDialog(this, RQS_ErrorDialog).show();
} else {
Toast.makeText(this,"YouTubePlayer.onInitializationFailure(): " + youTubeInitializationResult.toString(),Toast.LENGTH_LONG).show();
}
}
private final class MyPlayerStateChangeListener implements YouTubePlayer.PlayerStateChangeListener {
private void updateLog(String prompt){
log += "MyPlayerStateChangeListener" + "\n" +prompt + "\n\n=====";
textVideoLog.setText(log);
};
#Override
public void onAdStarted() {
updateLog("onAdStarted()");
}
#Override
public void onError(
com.google.android.youtube.player.YouTubePlayer.ErrorReason arg0) {
updateLog("onError(): " + arg0.toString());
}
#Override
public void onLoaded(String arg0) {
updateLog("onLoaded(): " + arg0);
}
#Override
public void onLoading() {
updateLog("onLoading()");
}
#Override
public void onVideoEnded() {
updateLog("onVideoEnded()");
}
#Override
public void onVideoStarted() {
updateLog("onVideoStarted()");
}
}
private final class MyPlaybackEventListener implements YouTubePlayer.PlaybackEventListener {
private void updateLog(String prompt){
log += "MyPlaybackEventListener" + "\n-" +
prompt + "\n\n=====";
textVideoLog.setText(log);
};
#Override
public void onBuffering(boolean arg0) {
updateLog("onBuffering(): " + String.valueOf(arg0));
}
#Override
public void onPaused() {
updateLog("onPaused()");
}
#Override
public void onPlaying() {
updateLog("onPlaying()");
}
#Override
public void onSeekTo(int arg0) {
updateLog("onSeekTo(): " + String.valueOf(arg0));
}
#Override
public void onStopped() {
updateLog("onStopped()");
}
}

Categories

Resources