【我的Android进阶之旅】解决MediaPlayer播放音乐的时候报错: Should have subtitle controller already set

简介: 一错误描述二错误解决解决方法一解决方法二一、错误描述刚用MediaPlayer播放Music的时候,看到Log打印台总是会打印一条错误日志,MediaPlayer: Should have subtitle controller already set,虽然程序运行不会出问题,但是看起来红色的日志很显眼,因此决定修改这个bug。

一、错误描述

刚用MediaPlayer播放Music的时候,看到Log打印台总是会打印一条错误日志,MediaPlayer: Should have subtitle controller already set,虽然程序运行不会出问题,但是看起来红色的日志很显眼,因此决定修改这个bug。具体的错误日志如下所示:

这里写图片描述

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

二、错误解决

通过google,在stackoverflow网站上搜索到了答案,相关链接如下所示:
http://stackoverflow.com/questions/20087804/should-have-subtitle-controller-already-set-mediaplayer-error-android

解决方法一

忽略此警告Log,因为并不影响程序正常运行。

正如stackoverflow网站上的答案说说的

A developer recently added subtitle support to VideoView.

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”.

解决方法二

如果MediaPlayer仅仅是用于播放音频文件,并且你真的想消除这些错误log的打印,则参考下面的代码来获取MediaPlayer。

   /**
     *  博客地址: 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的工作:

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

然后将初始化MediaPlayer的代码由:

 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

这里写图片描述

相关文章
|
3月前
|
Java 数据库连接
nacos2.0.3报错No Datasource Set
nacos2.0.3报错No Datasource Set com.mysql.cj.exceptions.CJException: Public Key Retrieval is not allowed
nacos2.0.3报错No Datasource Set
|
13天前
|
Java Maven Spring
【SpringBug】lombok插件失效,但是没有报错信息,@Data不能生成get和set方法
解决写了@Data注解,但是在测试文件中生成的反编译target文件Us二Info中没有get和set方法
117 15
|
2月前
|
JSON Java 关系型数据库
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
在Java中,使用mybatis-plus更新实体类对象到mysql,其中一个字段对应数据库中json数据类型,更新时报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
143 4
Java更新数据库报错:Data truncation: Cannot create a JSON value from a string with CHARACTER SET 'binary'.
|
5月前
|
网络安全 图形学 Android开发
Unity与安卓丨AS报错:SSL peer shut down incorrectly
Unity与安卓丨AS报错:SSL peer shut down incorrectly
Unity与安卓丨AS报错:SSL peer shut down incorrectly
|
5月前
|
开发工具 图形学 Android开发
Unity与安卓丨unity报错:SDK Tools version 0.0 < 26.1.1
Unity与安卓丨unity报错:SDK Tools version 0.0 < 26.1.1
|
5月前
|
API Android开发 图形学
UNITY与安卓⭐三、安卓报错答疑合集
UNITY与安卓⭐三、安卓报错答疑合集
|
5月前
|
Java Android开发
解决Android编译报错:Unable to make field private final java.lang.String java.io.File.path accessible
解决Android编译报错:Unable to make field private final java.lang.String java.io.File.path accessible
737 1
|
5月前
|
Kubernetes Unix 容器
As the default settings are now deprecated, you should set the endpoint inste
As the default settings are now deprecated, you should set the endpoint inste
|
1月前
|
算法
你对Collection中Set、List、Map理解?
你对Collection中Set、List、Map理解?
64 18
你对Collection中Set、List、Map理解?
|
26天前
|
存储 缓存 安全
只会“有序无序”?面试官嫌弃的List、Set、Map回答!
小米,一位热衷于技术分享的程序员,通过与朋友小林的对话,详细解析了Java面试中常见的List、Set、Map三者之间的区别,不仅涵盖了它们的基本特性,还深入探讨了各自的实现原理及应用场景,帮助面试者更好地准备相关问题。
56 20