我正在尝试使用Youtube API在android应用中播放youtube视频,以下是我编写的代码
activity_fetch_video.xml
<?xml version="1.0" encoding="utf-8"?>
<layout 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.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/youtubeFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top|center"
></FrameLayout>
</android.support.design.widget.CoordinatorLayout>
</layout>
在Java中,这是初始化YouTubePlayerSupportFragment并添加视频ID的方式。 FetchVideoActivity.java
private void initUI(){
YouTubePlayerSupportFragment youTubePlayerFragment =new YouTubePlayerSupportFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.youtubeFragment, youTubePlayerFragment);
fragmentTransaction.commit();
youTubePlayerFragment.initialize(YoutubeConstants.YOUTUBE_API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo(sVideoId);
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Log.d(TAG, "onInitializationFailure: ************************"+youTubeInitializationResult.toString());
}
});
}
我能够使用我的API_KEY初始化片段,然后片段开始播放视频。但是25秒后,该活动崩溃了。请参阅下面的日志:
2019-02-25 07:50:10.440 1281-1868/? I/ActivityManager: Force stopping com.google.android.youtube appid=10081 user=0: from pid 1281
2019-02-25 07:50:10.441 1281-1868/? I/ActivityManager: Killing 3189:com.google.android.youtube/u0a81 (adj 900): stop com.google.android.youtube
2019-02-25 07:50:10.446 1281-1868/? D/ActivityManager: Process com.google.android.youtube has 0 services
2019-02-25 07:50:10.455 1281-1868/? D/EmbryoManager: prepare com.google.android.youtube user 0
2019-02-25 07:50:10.456 1281-1868/? I/ActivityManager: Killing 3165:com.google.android.youtube.player/u0a81 (adj 0): stop com.google.android.youtube
2019-02-25 07:50:10.462 1281-1868/? D/ActivityManager: Process com.google.android.youtube.player has 1 services
2019-02-25 07:50:10.463 1281-1868/? W/ActivityManager: Scheduling restart of crashed service com.google.android.youtube/.api.service.YouTubeService in 1000ms
2019-02-25 07:50:10.468 1281-1868/? I/ActivityManager: Killing 3055:my.app.package/u0a1047 (adj 0): stop com.google.android.youtube
2019-02-25 07:50:10.470 1281-1868/? W/ActivityManager: Force removing ActivityRecord{71579f2 u0 my.app.package/.FetchVideoActivity t159}: app died, no saved state
2019-02-25 07:50:10.477 1281-1868/? I/WindowManager: Failed to capture screenshot of Token{278d843 ActivityRecord{71579f2 u0 cmy.app.package/.FetchVideoActivity t159 f}} appWin=Window{6dc7c16 u0 my.app.package/.FetchVideoActivity} drawState=4
2019-02-25 07:50:10.485 577-868/? E/SurfaceFlinger: Failed to find layer (my.app.package/my.app.package.FetchVideoActivity#0) in layer parent (no-parent).
2019-02-25 07:50:10.524 1281-2919/? D/MediaSessionService: Destroying com.google.android.youtube/YouTube playerlib (userId=0)
我有什么想念的吗?我已经浪费了很多时间解决此问题。