I was just upgrading old android project and stuck at below code :
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.instructional_tutorial_video_youtube_view, youTubePlayerFragment).commit();
return view;
Here, It giving me a compile time error : can not resolve method add(int, com.google.android.youtube.player.YouTubePlayerSupportFragment)
The Xml file named R.id.instructional_tutorial_video_youtube_view is as below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/test"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.brian.skyazul.fragment.InstructionTutorialVideoViewFragment">
<include
android:id="@ id/video_topbar"
layout="@layout/topbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="@ id/instructional_tutorial_video_youtube_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@ id/video_topbar"
android:layout_centerInParent="true"
android:layout_gravity="center" />
</RelativeLayout>
What might causing the issue? Or Is there any alternative to resolve this? thanks.
CodePudding user response:
fragment tag instead of YoutubePlayerView tag
<fragment
android:id="@ id/youtubeFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.google.android.youtube.player.YouTubePlayerFragment">
I haven't used before, I hope playerview it's in fragment itself.
In your fragment class
val youtubeFragment =
childFragmentManager.findFragmentById(R.id.youtubeFragment) as YouTubePlayerFragment
youtubeFragment.initialize("YOUR API KEY",
object : OnInitializedListener() {
fun onInitializationSuccess(
provider: YouTubePlayer.Provider?,
youTubePlayer: YouTubePlayer, b: Boolean
) {
// do any work here to cue video, play video, etc.
youTubePlayer.cueVideo("")
}
fun onInitializationFailure(
provider: YouTubePlayer.Provider?,
youTubeInitializationResult: YouTubeInitializationResult?
) {
}
})
CodePudding user response:
Instead of using this, I just used :
implementation 'com.pierfrancescosoffritti.androidyoutubeplayer:core:11.0.1'
<com.pierfrancescosoffritti.androidyoutubeplayer.core.player.views.YouTubePlayerView
android:id="@ id/instructional_tutorial_video_youtube_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@ id/video_topbar"
android:layout_centerInParent="true"
android:layout_gravity="center" />
and inside Java :
YouTubePlayerView youTubePlayerView = findViewById(R.id.instructional_tutorial_video_youtube_view);
youTubePlayerView.addYouTubePlayerListener(new AbstractYouTubePlayerListener() {
@Override
public void onReady(@NonNull YouTubePlayer youTubePlayer) {
String videoId = youtubeVideoURL;//"S0Q4gqBUs7c";
youTubePlayer.loadVideo(videoId, 0);
}
});
and it worked like a charm!