解决MediaPlayer异常: Should have subtitle controller already set

如果需要源码讲解或者其他问题可以私信找我

原因分析

1.MediaPlayer: Should have subtitle controller already set
首先出现的这个问题在API19与在API21以上是有区别的

API 21以上有以下两原因:
1.是由于未设置字幕
2.使用了MediaPlayer.create
解决方式: 设置字幕或者使用MediaPlayer mediaplayer = new MediaPlayer();

// 字幕文件路径
String textPath = dirFile.getAbsolutePath()+
                        File.separator +
                        "Music" +
                        File.separator +
                       fileName;

// 音频文件路径
String musicPath = dirFile.getAbsolutePath()+
                        File.separator +
                        "Music" +
                        File.separator +
                        musicName;


File musicFile = new File(musicPath);

                try {
                    mediaPlayer.setDataSource(this,Uri.fromFile(musicFile));
					// 这里设置字幕文件
                    mediaPlayer.addTimedTextSource(textPath, MediaPlayer.MEDIA_MIMETYPE_TEXT_SUBRIP);
                    TrackInfo[] trackInfos = mediaPlayer.getTrackInfo();
                    if (trackInfos != null && trackInfos.length > 0){
                        for (int i = 0; i < trackInfos.length; i++){
                            final TrackInfo info = trackInfos[i];
                            Log.v(TAG, "TrackInfo: " + info.getTrackType() + " "+ info.getLanguage());
                            if (info.getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_AUDIO){
                                // mMediaPlayer.selectTrack(i);
                            }
                            else if (info.getTrackType() == TrackInfo.MEDIA_TRACK_TYPE_TIMEDTEXT){
                                mediaPlayer.selectTrack(i);
                            }
                        }
                    }

					// 监听歌词信息
                    mediaPlayer.setOnTimedTextListener(new MediaPlayer.OnTimedTextListener() {
                        @Override
                        public void onTimedText(MediaPlayer mp, TimedText text) {
                            if (text != null) {
                                Log.v(getPackageName(),"getText" + text.getText());
                            }
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }

字幕自动播放效果:
解决MediaPlayer异常: Should have subtitle controller already set_第1张图片
API 21以下的:
如果设置了字幕,那么就会报MediaPlayer: info/warning (900, -1007)的异常,大概就会认为媒体信息计时文本错误不对,这个原因待分析,大概猜测为API 19 不支持字幕?,因此想要解决 Should have subtitle controller already set 异常只能通过反射来解决

private 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;
    }

你可能感兴趣的:(解决MediaPlayer异常: Should have subtitle controller already set)