Android YouTube API “初始化 YouTube 播放器时出错”
Android YouTube API “An error occurred while initializing YouTube player”
我有一个包含 YouTubePlayerSupportFragment 的片段。此片段位于 ViewPager 内。
播放器在我的 Galaxy S3 Android phone (4.3 / API 18) 和我的虚拟机 (4.3 / API 18) 上运行良好。
但是在我尝试过的所有其他 phone 中,玩家都会显示 An error occurred while initializing YouTube player
消息并抛出 onInitializationFailure 并给出 UNKNOWN_ERROR
原因。
我已经检查了 LogCat,绝对没有其他 error/warning 来自我的应用程序或来自 youtube。
它不适用于以下设备:
- Android 4.2.1 - YouTube 10.18.55
- Android 4.4.2 - YouTube 10.18.55
- Android 5.1 - Youtube 高于要求
我完全不知道为什么它对某些人有用而对其他人却没有。
片段代码如下:
public class RoomFragmentVideo extends PlugFragment implements
YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener {
private static String CLASS_DBG_NAME = "RoomFragmentVideo";
private static final int RECOVERY_DIALOG_REQUEST = 1;
private YouTubePlayerSupportFragment youTubePlayer ;
private int playerInitialized;
private YouTubePlayer player;
static PlugFragment init(Bundle args) {
RoomFragmentVideo roomFrag = new RoomFragmentVideo();
// Supply val input as an argument.
roomFrag.setArguments(args);
Log.d(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - ","Created with args Room: " + ((String)args.get(RoomFragmentChat.ARG_ROOM)));
return roomFrag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thisActivity = (AppCompatActivity) this.getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_roomvideo, container,false);
youTubePlayer = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayer).commit();
youTubePlayer.initialize(YouTubeDeveloperKey.DEVELOPER_KEY, this);
return rootview;
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return youTubePlayer;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
getYouTubePlayerProvider().initialize(YouTubeDeveloperKey.DEVELOPER_KEY, this);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this.getActivity(), 1).show();
Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - YouTube" ,"[Recoverable] Reason : " + errorReason.toString());
}
else {
//String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
String errorMessage = "Video couldnt load";
Toast.makeText(this.getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - YouTube" ,"[Unrecoverable] Reason : " + errorReason.toString() + " Message: " + errorMessage);
playerInitialized = -1;
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
this.player = player;
player.setPlayerStyle(PlayerStyle.CHROMELESS);
player.loadVideo(this.videoID);
playerInitialized = 1;
}
@Override
public void onPause(){
super.onPause();
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onPause");
}
@Override
public void onResume(){
super.onResume();
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onResume");
}
@Override
public void onStop(){
super.onStop();
player = null;
playerInitialized = -2;
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onStop");
}
@Override
public void setUserVisibleHint (boolean isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
this.resume();
}
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "setUserVisibleHint, status: " + isVisibleToUser);
}
@Override
public void onAdStarted() {
return;
}
@Override
public void onError(ErrorReason arg0) {
Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " +"YoutubePlayer", arg0.name());
Toast.makeText(thisActivity, "YoutubePlayer error: " + arg0.name(), Toast.LENGTH_SHORT).show();
}
@Override
public void onLoaded(String arg0) {
return;
}
@Override
public void onLoading() {
return;
}
@Override
public void onVideoEnded() {
return;
}
@Override
public void onVideoStarted() {
return;
}
}
这里是 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/youtube_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:visibility="visible" />
</LinearLayout>
我做错了什么?
非常感谢!
问题实际上是proguard。
这是我添加到 proguard-project.txt
文件的内容:
# keep youtube / google
-keep class com.google.api.services.** { *; }
-keep class com.google.android.youtube.player.** { *; }
# Needed by google-api-client to keep generic types and @Key annotations accessed via reflection
-keepclassmembers class * {
@com.google.api.client.util.Key <fields>;
}
这个对我有用:
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}
我有一个包含 YouTubePlayerSupportFragment 的片段。此片段位于 ViewPager 内。
播放器在我的 Galaxy S3 Android phone (4.3 / API 18) 和我的虚拟机 (4.3 / API 18) 上运行良好。
但是在我尝试过的所有其他 phone 中,玩家都会显示 An error occurred while initializing YouTube player
消息并抛出 onInitializationFailure 并给出 UNKNOWN_ERROR
原因。
我已经检查了 LogCat,绝对没有其他 error/warning 来自我的应用程序或来自 youtube。
它不适用于以下设备:
- Android 4.2.1 - YouTube 10.18.55
- Android 4.4.2 - YouTube 10.18.55
- Android 5.1 - Youtube 高于要求
我完全不知道为什么它对某些人有用而对其他人却没有。
片段代码如下:
public class RoomFragmentVideo extends PlugFragment implements
YouTubePlayer.OnInitializedListener, YouTubePlayer.PlayerStateChangeListener {
private static String CLASS_DBG_NAME = "RoomFragmentVideo";
private static final int RECOVERY_DIALOG_REQUEST = 1;
private YouTubePlayerSupportFragment youTubePlayer ;
private int playerInitialized;
private YouTubePlayer player;
static PlugFragment init(Bundle args) {
RoomFragmentVideo roomFrag = new RoomFragmentVideo();
// Supply val input as an argument.
roomFrag.setArguments(args);
Log.d(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - ","Created with args Room: " + ((String)args.get(RoomFragmentChat.ARG_ROOM)));
return roomFrag;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
thisActivity = (AppCompatActivity) this.getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_roomvideo, container,false);
youTubePlayer = YouTubePlayerSupportFragment.newInstance();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.youtube_fragment, youTubePlayer).commit();
youTubePlayer.initialize(YouTubeDeveloperKey.DEVELOPER_KEY, this);
return rootview;
}
protected YouTubePlayer.Provider getYouTubePlayerProvider() {
return youTubePlayer;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RECOVERY_DIALOG_REQUEST) {
getYouTubePlayerProvider().initialize(YouTubeDeveloperKey.DEVELOPER_KEY, this);
}
}
@Override
public void onInitializationFailure(YouTubePlayer.Provider provider,
YouTubeInitializationResult errorReason) {
if (errorReason.isUserRecoverableError()) {
errorReason.getErrorDialog(this.getActivity(), 1).show();
Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - YouTube" ,"[Recoverable] Reason : " + errorReason.toString());
}
else {
//String errorMessage = String.format(getString(R.string.error_player), errorReason.toString());
String errorMessage = "Video couldnt load";
Toast.makeText(this.getActivity(), errorMessage, Toast.LENGTH_LONG).show();
Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - YouTube" ,"[Unrecoverable] Reason : " + errorReason.toString() + " Message: " + errorMessage);
playerInitialized = -1;
}
}
@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer player,
boolean wasRestored) {
this.player = player;
player.setPlayerStyle(PlayerStyle.CHROMELESS);
player.loadVideo(this.videoID);
playerInitialized = 1;
}
@Override
public void onPause(){
super.onPause();
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onPause");
}
@Override
public void onResume(){
super.onResume();
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onResume");
}
@Override
public void onStop(){
super.onStop();
player = null;
playerInitialized = -2;
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "onStop");
}
@Override
public void setUserVisibleHint (boolean isVisibleToUser){
super.setUserVisibleHint(isVisibleToUser);
if(isVisibleToUser){
this.resume();
}
Log.v(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " , "setUserVisibleHint, status: " + isVisibleToUser);
}
@Override
public void onAdStarted() {
return;
}
@Override
public void onError(ErrorReason arg0) {
Log.e(Constants.APP_DBG_LOG + CLASS_DBG_NAME + " - " +"YoutubePlayer", arg0.name());
Toast.makeText(thisActivity, "YoutubePlayer error: " + arg0.name(), Toast.LENGTH_SHORT).show();
}
@Override
public void onLoaded(String arg0) {
return;
}
@Override
public void onLoading() {
return;
}
@Override
public void onVideoEnded() {
return;
}
@Override
public void onVideoStarted() {
return;
}
}
这里是 XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/youtube_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:visibility="visible" />
</LinearLayout>
我做错了什么?
非常感谢!
问题实际上是proguard。
这是我添加到 proguard-project.txt
文件的内容:
# keep youtube / google
-keep class com.google.api.services.** { *; }
-keep class com.google.android.youtube.player.** { *; }
# Needed by google-api-client to keep generic types and @Key annotations accessed via reflection
-keepclassmembers class * {
@com.google.api.client.util.Key <fields>;
}
这个对我有用:
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}