【我的Android进阶之旅】解决MediaPlayer播放音乐的时候报错: Should have subtitle controller already set
一、错误描述
刚用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
【我的Android进阶之旅】解决MediaPlayer播放音乐的时候报错: Should have subtitle controller already set的更多相关文章
- 我的Android进阶之旅------>解决Jackson等第三方转换Json的开发包在开启混淆后转换的实体类数据都是null的bug
1.错误描述 今天测试人员提了一个bug,说使用我们的app出现了闪退的bug,后来通过debug断点调试,发现我们的app转换服务器发送过来的json数据后,都是为null.而之前已经提测快一个月的 ...
- 我的Android进阶之旅------>解决Android Studio编译后安装apk报错:The APK file does not exist on disk
1.错误描述 今天用Android Studio编译应用后安装APK的时候,报错了,错误如下所示: The APK file build\outputs\apk\OYP_2.3.4_I2Base_64 ...
- 我的Android进阶之旅------>解决AES加密报错:java.security.InvalidKeyException: Unsupported key size: 18 bytes
1.错误描述 今天使用AES进行加密时候,报错如下所示: 04-21 11:08:18.087 27501-27501/com.xtc.watch E/AESUtil.decryptAES:55: j ...
- 我的Android进阶之旅------>解决:Failed to create 'build\outputs\apk\watch-debug-unaligned.apks': 拒绝访问。
1. 错误描述 今天用Android Studio进行项目编译的时候,报错如下所示: FAILURE: Build failed with an exception. * What went wron ...
- 我的Android进阶之旅------>解决Your project contains error(s),please fix them
在使用eclipse写好Android的代码,代码没有报错.然后想在AVD中运行测试时,弹出错误框,提示信息为: "Your project contains error(s),pleas ...
- 我的Android进阶之旅------>解决错误: java.util.regex.PatternSyntaxException: Incorrect Unicode property
1.错误描述 今天使用正则表达式验证密码的时候,报了错误 java.util.regex.PatternSyntaxException: Incorrect Unicode property near ...
- 我的Android进阶之旅------>解决如下错误failed to copy 'Settings2.apk' to '/system/app//Settings2.apk': Read-only
push apk的时候报错 ouyangpeng@oyp-ubuntu:~/apk升级$ adb push Settings2.apk /system/app/ failed to copy 'Set ...
- 我的Android进阶之旅------>解决DownloadManager报错java.lang.SecurityException: Invalid value for visibility: 2
1.问题描述 今天使用Android系统的DownloadManager进行下载操作时,爆了如下所示的错误: java.lang.RuntimeException: Unable to start s ...
- 我的Android进阶之旅------>解决Error: specified for property 'mergedManifest' does not exist.
错误描述 刚运行从Github上面下载而来的代码时,爆了如下所示的bug,错误描述如下所示: Error:A problem was found with the configuration of t ...
随机推荐
- python操作sqlserver
# coding=gbk import sys import pymssql #尝试数据库连接 try: conn = pymssql.connect(host="192.168 ...
- iOS开发多线程篇 03 —线程安全
iOS开发多线程篇—线程安全 一.多线程的安全隐患 资源共享 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象.同一个变量.同一个文件 当多个线程访问同一块 ...
- .net获取客户端IP
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Secu ...
- :(23, 7) in class Queen, multiple overloaded alternatives of constructor Queen define default arguments. class Queen private(val name:String,prop:Array[String],private[scala02] val age:Int = 18){
- 简单的SpringCloud 熔断Hystrix
pom配置 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId> ...
- c# 实现遍历 DataTable 和DataSet (简单的方式)
今天 做一个小程序 ,遇到了这样一个 问题就是 怎样简单的 遍历一个 DataTable 一. DataTable table= DBhelper.GetDataTable(str);foreach( ...
- Android 消息处理源代码分析(2)
Android 消息处理源代码分析(1)点击打开链接 继续接着分析剩下的类文件 Looper.java public final class Looper { final MessageQueue m ...
- smartJS 0.1 API 讲解 - Trigger
上篇介绍了PromiseEvent,本篇介绍Trigger - 基于Promise的aop的体现:(感觉自己的对这些命名一直都很挫,也懒得想了,所以就凑合的用) Trigger 在目标对象上加入触发器 ...
- vc6项目-vc8项目 转换日志
此随笔参考了http://blog.163.com/feng_qihang/blog/static/7129199120093422722430/ 把VC6的项目转换成VS2005项目,经过VS200 ...
- 虚拟机安装Ubuntu过程记录
1.WMware中新建虚拟机 2.选择安装程序光盘镜像iso 3.个性化Linux(全名.用户名.密码等) 4.指定虚拟机名称以及安装位置 5.指定虚拟机磁盘容量大小 6.完成虚拟机配置 安装过程.. ...