【個人Android進階之旅】解決MediaPlayer播放音樂的時候報錯: Should have subtitle controller already set

1、錯誤描述

剛用MediaPlayer播放Music的時候,看到Log打印臺老是會打印一條錯誤日誌,MediaPlayer: Should have subtitle controller already set,雖然程序運行不會出問題,可是看起來紅色的日誌很顯眼,所以決定修改這個bug。具體的錯誤日誌以下所示:css

這裏寫圖片描述

01-04 17:46:21.752 9395-9395/com.netease.xtc.cloudmusic I/CloudMusicPlayService: >>>>>>>>>>>>>>> mediaPlayer onBufferingUpdate
01-04 17:46:21.752 9395-9395/com.netease.xtc.cloudmusic D/MediaPlayer: handleMessage msg:(1, 0, 0)
01-04 17:46:21.753 9395-9395/com.netease.xtc.cloudmusic E/MediaPlayer: Should have subtitle controller already set
01-04 17:46:21.753 9395-9395/com.netease.xtc.cloudmusic I/CloudMusicPlayService: >>>>>>>>>>>>>>> mediaPlayer onPrepared
01-04 17:46:21.756 9395-9395/com.netease.xtc.cloudmusic I/CloudMusicPlayService: >>>>>>>>>>>>>>> startPlay

2、錯誤解決

經過google,在stackoverflow網站上搜索到了答案,相關連接以下所示:
http://stackoverflow.com/questions/20087804/should-have-subtitle-controller-already-set-mediaplayer-error-androidjava

解決方法一

忽略此警告Log,由於並不影響程序正常運行。android

正如stackoverflow網站上的答案說說的markdown

A developer recently added subtitle support to VideoView.ide

When the MediaPlayer starts playing a music (or other source), it checks if there is a SubtitleController and shows this message if it’s not set. It doesn’t seem to care about if the source you want to play is a music or video. Not sure why he did that.網站

Short answer: Don’t care about this 「Exception」.ui

解決方法二

若是MediaPlayer僅僅是用於播放音頻文件,而且你真的想消除這些錯誤log的打印,則參考下面的代碼來獲取MediaPlayer。this

/** * 博客地址: http://blog.csdn.net/ouyang_peng/ * 獲取MediaPlayer 修復bug ( MediaPlayer: Should have subtitle controller already set ) * </br><a href = "http://stackoverflow.com/questions/20087804/should-have-subtitle-controller-already-set-mediaplayer-error-android/20149754#20149754"> * 參考連接</a> * * </br> This code is trying to do the following from the hidden API * <p> * </br> SubtitleController sc = new SubtitleController(context, null, null); * </br> sc.mHandler = new Handler(); * </br> mediaplayer.setSubtitleAnchor(sc, null)</p> */
    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);
        } catch (Exception e) {
            LogUtil.d(TAG,"getMediaPlayer crash ,exception = "+e);
        }
        return mediaplayer;
    }

這裏寫圖片描述

上面代碼的做用是作相似於下面隱藏API的工做:google

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

而後將初始化MediaPlayer的代碼由:spa

 MediaPlayer mediaPlayer = new MediaPlayer();

改成:

MediaPlayer mediaPlayer = getMediaPlayer(this);

這裏寫圖片描述

而後從新運行APP以後就不會再出現以前的紅色警告log MediaPlayer: Should have subtitle controller already set了。


做者:歐陽鵬 歡迎轉載,與人分享是進步的源泉!
轉載請保留原文地址:
http://blog.csdn.net/ouyang_peng/article/details/54023240

這裏寫圖片描述

相關文章
相關標籤/搜索