您的位置:首页 > 移动开发 > Android开发

android播放音视频使用mediaplayer不规范导致的电流过高问题

2014-01-12 12:07 519 查看
在使用mediaplayer等多媒体相关的类进行多媒体应用开发的时候,建议各位小伙伴仔细阅读sdk,按照标准使用,曾经遇到一个问题,某某同学在修改音视频code时,为了方便,在activity的onPause()和onStop()时并未进行资源的回收,即调用mediaplayer的release()方法释放,这样,在重新onResume()或者onStart()的时候就不需要重新new实例,重新设置Uri等一系列操作,看起来是方便许多,其实这里隐藏这一个较大的问题,查看sdk,可以看到如下一段话:


public void release ()

Added in API level 1

Releases resources associated with this MediaPlayer object. It is considered good practice to call this method when you're done using the MediaPlayer. In particular, whenever
an Activity of an application is paused (its onPause() method is called), or stopped (its onStop() method is called), this method should be invoked to release the MediaPlayer object, unless the application has a special need to keep the object around. In addition
to unnecessary resources (such as memory and instances of codecs) being held, failure to call this method immediately if a MediaPlayer object is no longer needed may also
lead to continuous battery consumption for mobile devices,
and playback failure for other applications if no multiple instances of the same codec are supported on a device. Even if multiple instances of the same codec are supported,
some performance degradation may be expected when unnecessary multiple instances are used at the same time.

如果不释放,在应用长期后台的情况下,待机电流过高或者media中的codec实例被过度消耗导致其他应用播放失败, 手机性能降低等等一系列问题,在debug时也难以分析,只有痛苦的review代码,原本很多只是多阅读一下api的问题,却要耗费大量时间debug,真是得不偿失,OEM厂商在只能控制内置应用的规范,如果因为三方软件的不规范开发导致的电流过高或者其他应用异常,导致待机时间减少,手机厂商就是真的躺枪了,android的碎片化已经是一个客观存在的问题,我们只能要求自己有良好的编程习惯了。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐