我有同样的问题 - 我想YouTube播放器添加到我的应用程序,但桦林我并不想从它删除夏洛特(基于支持库)。什么是坏的,我没有能够使用任何playbers,因为我有错误(膨胀片段,YouTubePlayerView无法启动没有特殊的活动等)。
工作原理:我使用了SherlockFragmentActivity,FragmentManager(getSupportFragmentManager())和YouTubePlayerSupportFragment。我没有将它添加到XML中,而是从代码创建了一切。我的布局是这样的:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/fragmentz"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
和Java代码:
package com.example.youtubetesting;
import com.actionbarsherlock.app.SherlockFragmentActivity;
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.YouTubePlayerSupportFragment;
import com.google.android.youtube.player.YouTubePlayer.Provider;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class MainActivity extends SherlockFragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
YouTubePlayerSupportFragment fragment = new YouTubePlayerSupportFragment();
fragmentTransaction.add(R.id.fragmentz, fragment);
fragmentTransaction.commit();
fragment.initialize("Your API KEY HERE",
new OnInitializedListener() {
@Override
public void onInitializationSuccess(Provider arg0,
YouTubePlayer arg1, boolean arg2) {
if (!arg2) {
arg1.loadVideo("wKJ9KzGQq0w");
}
}
@Override
public void onInitializationFailure(Provider arg0,
YouTubeInitializationResult arg1) {
}
});
}
}
我不知道为什么Android的返航错误,当我在不断膨胀的正常方式的意见,但这种完美的作品。
这个和juandg的答案似乎是正确的。这个答案提供了一些具体的例子,所以我标记了达米安最完整的。 – kittka
你有没有正确使用此全屏? –