我正在尝试在我的Android应用程序中实现Youtube API,该应用程序使用viewpager来显示视频和一些相关信息。我使用webview实现了相同的功能,但无法使用YouTube API。任何人都可以帮我解决问题吗? 主要错误是:
MainActivity:
public class MainActivity extends AppCompatActivity{
.
.
.
protected void onCreate(Bundle savedInstanceState) {
.
.
.
youTubePlayerFragment = (YouTubePlayerSupportFragment) getSupportFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize(Config.YOUTUBE_API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
player.cueVideo(getResources().getString(R.string.vid1));
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider arg0, YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
});
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
}
}
viewpager中要显示视频的片段:
public class VideoFragment extends Fragment implements
YouTubePlayer.OnInitializedListener{
.
.
.
//Returns fragment to be shown in the viewpager fragment
YTfrag f = YTfrag.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.youtube_fragment, f).commit();
}
public static VideoFragment newInstance(String title, String desc, String link, String narrator, int narratorImage) {
VideoFragment f = new VideoFragment();
Bundle b = new Bundle();
b.putString("title", title);
b.putString("desc", desc);
b.putString("link", link);
b.putString("narrator", narrator);
b.putInt("narratorImage", narratorImage);
f.setArguments(b);
return f;
}
YTfrag片段:
public class YTfrag extends YouTubePlayerSupportFragment {
public YTfrag() {
}
public static YTfrag newInstance() {
//Bundle b = new Bundle();
// b.putString("video", video);
//f.setArguments(b);
//f.init();
return new YTfrag();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
View v = inflater.inflate(R.layout.fragment_video, container, false);
YouTubePlayerSupportFragment youTubePlayerSupportFragment = (YouTubePlayerSupportFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
initialize(Config.YOUTUBE_API_KEY, new YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
try {
youTubePlayer.cueVideo(getResources().getString(R.string.vid1));
} catch (NullPointerException e) {
e.printStackTrace();
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
Toast.makeText(getContext(), "Cant play video", Toast.LENGTH_SHORT).show();
Log.e("MainActivity", "onInitializationFailure");
}
});
return v;
}
viewpager片段布局:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:autofit="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/viewA"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.6"
android:orientation="horizontal">
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout
android:id="@+id/viewB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:layout_weight="0.4"
android:orientation="vertical">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".92">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<me.grantland.widget.AutofitTextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:gravity="fill_horizontal"
android:maxLines="3"
android:minLines="1"
android:textSize="19dp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/primary_text" />
<me.grantland.widget.AutofitTextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:minLines="1"
android:maxLines="9"
android:gravity="fill_horizontal"
android:textSize="17dp" />
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_weight=".08"
android:baselineAligned="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/narratorImageIV"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="center"
android:background="@color/transparent"
android:contentDescription="NarratorImagesImageView"
android:gravity="center"
android:scaleType="fitCenter" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:text="Memed "
android:textColor="@color/colorPrimary"
android:textSize="12dp"
android:textStyle="bold" />
<TextView
android:id="@+id/narratorNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text=""
android:textColor="@color/colorPrimary"
android:textSize="12dp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
非常感谢任何形式的帮助。