У меня есть фрагмент, и я хочу воспроизвести видео YouTube внутри фрагмента. Для этого я должен выполнить следующие шаги
- Создан проект на консоли Google
- Создать ключ
- Загрузите и установите YouTubeAndroidPlayerApi.
- Создайте простой фрагмент
- Добавьте YouTubePlayerSupportFragment
- Напишите код в java файле
Когда я запускаю приложение, получаю сообщение об ошибке
Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.google.android.youtube.player.YouTubePlayerView
Caused by: java.lang.IllegalStateException: A YouTubePlayerView can only be created with an Activity which extends YouTubeBaseActivity as its context.
код XML файл
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.google.android.youtube.player.YouTubePlayerView
android:name="com.google.android.youtube.player.
YouTubePlayerSupportFragment"
android:id="@+id/YoutubePlayer"
android:layout_width="match_parent"
android:layout_height="240dp"
android:layout_marginTop="20dp"/>
</LinearLayout>
код Java файл
public class TopicYoutubeFragment extends Fragment implements YouTubePlayer.OnInitializedListener {
private YouTubePlayer youtubeplayer;
private static final String YoutubeDeveloperKey = "AIzaSyAp8uMTh0VnYI";
private String YoutubeURL, youtubeID;
private String TopicName;
private static final int RECOVERY_REQUEST = 1;
private YouTubePlayerView ypv;
public TopicYoutubeFragment() {
}
public static TopicYoutubeFragment newInstance(String YoutubeURL, String TopicName) {
TopicYoutubeFragment fragment = new TopicYoutubeFragment();
Bundle args = new Bundle();
args.putString("YoutubeURL", YoutubeURL);
args.putString("TopicName", TopicName);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
YoutubeURL = getArguments().getString(YoutubeURL);
TopicName = getArguments().getString(TopicName);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.topicyoutubefragment, container, false);
ypv = view.findViewById(R.id.YoutubePlayer);
ypv.initialize(YoutubeDeveloperKey, this);
youtubeID = "F7xdbsjFGtU";
return view;
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
if (youtubeID != null)
youTubePlayer.cueVideo(youtubeID); // Plays https://www.youtube.com/watch?v=F7xdbsjFGtU
else
Toast.makeText(getActivity(), "No Video is available for this Topic", Toast.LENGTH_LONG).show();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(getActivity(), RECOVERY_REQUEST).show();
} else {
String error = String.format(getString(R.string.player_error), errorReason.toString());
Toast.makeText(getActivity(), error, Toast.LENGTH_LONG).show();
}
}
}