My Activity扩展了另一个活动,包含导航抽屉,每个项目都打开一个新片段。我想将Youtube集成到其中一个片段中。以前我使用YouTubePlayerView将Youtube集成到活动中,但这里需要片段。我在网上搜索,发现YouTubePlayerFragment将youtube整合到片段中。但是当我在教程中搜索时,我发现即使使用YouTubePlayerFragment,我们也在扩展YouTubeBaseActivity。这些都是示例.. http://android-coding.blogspot.in/2013/04/example-to-use-youtubeplayerfragment-of.html http://android-er.blogspot.in/2013/06/example-to-use-youtubeplayerfragment-of.html
我无法理解如何使用YouTubePlayerFragment,以便我的类扩展Fragment而不是我的项目中所需的YouTubeBaseActivity。如下所示,我的课程扩展了另一项活动并包含导航抽屉,其中第五个选项打开YouTube片段我想在这个片段中播放Youtube视频。我简要介绍了我的课程 -
public class LandingActivity extends BaseGActivity {
.
.
.
.
public void selectDrawerItem(int position) {
Bundle args = new Bundle();
switch (position) {
case 0:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_ALL);
GApplication.getInstance().stopGPlayer();
break;
case 1:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_PHOTO);
GApplication.getInstance().stopGPlayer();
break;
//
case 2:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_AUDIO);
GApplication.getInstance().stopGPlayer();
break;
case 3:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_VIDEO);
GApplication.getInstance().stopGPlayer();
break;
case 4:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_MEME);
GApplication.getInstance().stopGPlayer();
break;
case 5:
currentFragment = new YoutubeFragment();
default:
currentFragment = new HomeFragment_();
args.putString(G.General.MEDIA_TYPE_KEY, G.General.MEDIA_TYPE_ALL);
GApplication.getInstance().stopGPlayer();
break;
}
currentFragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, currentFragment)
.commit();
mDrawerList.setItemChecked(position, true);
setTitle(dataList.get(position).getItemName());
mDrawerLayout.closeDrawers();
}
public class YoutubeFragment extends Fragment implements YouTubePlayer.OnInitializedListener{
private FragmentActivity myContext;
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "xyz";
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
public void onAttach(Activity activity) {
if (activity instanceof FragmentActivity) {
myContext = (FragmentActivity) activity;
}
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_you_tube_api, container, false);
YouTubePlayerView youTubeView = (YouTubePlayerView) rootView.findViewById(R.id.youtube_view);
youTubeView.initialize(YoutubeDeveloperKey, (YouTubePlayer.OnInitializedListener) myContext);
return rootView;
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this, RECOVERY_DIALOG_REQUEST).show();
} else {
String errorMessage = String.format(
"There was an error initializing the YouTubePlayer",
errorReason.toString());
Toast.makeText(this, errorMessage, Toast.LENGTH_LONG).show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST)
{
getYouTubePlayerProvider().initialize(YoutubeDeveloperKey, this);
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider,
YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
YPlayer = player;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
}
YouTubeFragment.java
public class YoutubeFragment extends Fragment implements
YouTubePlayer.OnInitializedListener {
private FragmentActivity myContext;
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "xyz";
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
public void onAttach(Activity activity) {
if (activity instanceof FragmentActivity) {
myContext = (FragmentActivity) activity;
}
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_you_tube_api, container, false);
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
youTubePlayerFragment.initialize("DEVELOPER_KEY", new YouTubePlayer.OnInitializedListener() {
});
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
return rootView;
}
@Override
public void onInitializationSuccess (YouTubePlayer.Provider provider, YouTubePlayer
youTubePlayer,boolean b){
if (!b) {
YPlayer = youTubePlayer;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
@Override
public void onInitializationFailure (YouTubePlayer.Provider
provider, YouTubeInitializationResult youTubeInitializationResult){
}
}
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<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>
错误 -
Error:(64, 101) error: <anonymous com.pc.gi.ui.fragment.YoutubeFragment$1> is not abstract and does not override abstract method onInitializationFailure(Provider,YouTubeInitializationResult) in OnInitializedListener
答案 0 :(得分:69)
首先将您的活动正常扩展
class YourActivity extends Activity...
布局文件中的放下以下行
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerSupportFragment"
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
然后在您的Activity中,您可以使用片段的onCreareView方法中的下一行创建其实例。
YouTubePlayerSupportFragment youTubePlayerFragment = (YouTubePlayerSupportFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.youtube_fragment);
或者您可以在xml中声明FrameLayout,然后使用以下行启动YouTubeSupportFragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:visibility="visible" />
</RelativeLayout>
onCreateView中的代码
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
youTubePlayerFragment.initialize("DEVELOPER_KEY", new OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player, boolean wasRestored) {
if (!wasRestored) {
YPlayer = player;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
@Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
});
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
这里的关键是使用YouTubePlayerSupportFragment
代替YouTubePlayerFragment
。
希望这有帮助。
这是你的片段
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.youtube.player.YouTubeInitializationResult;
import com.google.android.youtube.player.YouTubePlayer;
import com.google.android.youtube.player.YouTubePlayer.OnInitializedListener;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import com.google.android.youtube.player.YouTubePlayerSupportFragment;
import com.ismart.omanapp.R;
public class YoutubeFragment extends Fragment {
private FragmentActivity myContext;
private YouTubePlayer YPlayer;
private static final String YoutubeDeveloperKey = "xyz";
private static final int RECOVERY_DIALOG_REQUEST = 1;
@Override
public void onAttach(Activity activity) {
if (activity instanceof FragmentActivity) {
myContext = (FragmentActivity) activity;
}
super.onAttach(activity);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_you_tube_api, container, false);
YouTubePlayerSupportFragment youTubePlayerFragment = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayerFragment).commit();
youTubePlayerFragment.initialize("DEVELOPER_KEY", new OnInitializedListener() {
@Override
public void onInitializationSuccess(Provider arg0, YouTubePlayer youTubePlayer, boolean b) {
if (!b) {
YPlayer = youTubePlayer;
YPlayer.setFullscreen(true);
YPlayer.loadVideo("2zNSgSzhBfM");
YPlayer.play();
}
}
@Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1) {
// TODO Auto-generated method stub
}
});
}
}
答案 1 :(得分:0)
要使用片段实现youtube播放器,请执行以下3个步骤:-
在活动XML文件中添加以下内容:
<fragment
android:name="com.google.android.youtube.player.YouTubePlayerFragment"
android:id="@+id/youtube_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
在活动Java文件中创建一个新的OnInitialize侦听器
YouTubePlayer.OnInitializedListener onInitializedListener = new
YouTubePlayer.OnInitializedListener() {
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {
youTubePlayer.loadVideo("Your_video_id");
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
}
};
初始化监听器后,获取片段引用并初始化播放器。
YouTubePlayerFragment youTubePlayerFragment = (YouTubePlayerFragment) getFragmentManager().findFragmentById(R.id.youtube_fragment);
youTubePlayerFragment.initialize("You_Developer_Api_Key", onInitializedListener);
要获取开发人员API密钥,请遵循此link。