E/MediaPlayer: Should have subtitle controller already set

时间:2022-03-17 15:07:34

原文链接:http://*.com/questions/20087804/should-have-subtitle-controller-already-set-mediaplayer-error-android/20149754#20149754

E/MediaPlayer: Should have subtitle controller already set

 static MediaPlayer getMediaPlayer(Context context){

     MediaPlayer mediaplayer = new MediaPlayer();

     if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
return mediaplayer;
} try {
Class<?> cMediaTimeProvider = Class.forName( "android.media.MediaTimeProvider" );
Class<?> cSubtitleController = Class.forName( "android.media.SubtitleController" );
Class<?> iSubtitleControllerAnchor = Class.forName( "android.media.SubtitleController$Anchor" );
Class<?> iSubtitleControllerListener = Class.forName( "android.media.SubtitleController$Listener" ); Constructor constructor = cSubtitleController.getConstructor(new Class[]{Context.class, cMediaTimeProvider, iSubtitleControllerListener}); Object subtitleInstance = constructor.newInstance(context, null, null); Field f = cSubtitleController.getDeclaredField("mHandler"); f.setAccessible(true);
try {
f.set(subtitleInstance, new Handler());
}
catch (IllegalAccessException e) {return mediaplayer;}
finally {
f.setAccessible(false);
} Method setsubtitleanchor = mediaplayer.getClass().getMethod("setSubtitleAnchor", cSubtitleController, iSubtitleControllerAnchor); setsubtitleanchor.invoke(mediaplayer, subtitleInstance, null);
//Log.e("", "subtitle is setted :p");
} catch (Exception e) {} return mediaplayer;
}

  This code is trying to do the following from the hidden API

 SubtitleController sc = new SubtitleController(context, null, null);
sc.mHandler = new Handler();
mediaplayer.setSubtitleAnchor(sc, null)