问题
I have a Fragment containing a YouTubePlayerSupportFragment. This fragment is inside a ViewPager.
The player works fine on my Galaxy S3 Android phone (4.3 / API 18) and on my VM (4.3 / API 18).
But on every other phones i've tried, the player shows a An error occurred while initializing YouTube player
message and throw a onInitializationFailure with an UNKNOWN_ERROR
reason.
I've checked the LogCat and there is absolutly no other error/warning, from my app or from youtube.
It didnt work on the following devices:
- Android 4.2.1 - Youtube 10.18.55
- Android 4.4.2 - Youtube 10.18.55
- Android 5.1 - Youtube above requirement
I have absolutely no idea why it worked on some and why it didn't on others.
Here is the code of the Fragment:
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;
}
}
And here is the 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>
What am I doing wrong ?
Thanks a lot !
回答1:
The problem was in fact proguard.
Here is what i added to the proguard-project.txt
file:
# 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>;
}
回答2:
This one worked for me:
-keepclassmembers class fqcn.of.javascript.interface.for.webview {
public *;
}
来源:https://stackoverflow.com/questions/30428535/android-youtube-api-an-error-occurred-while-initializing-youtube-player