单例模式是否应用于Android MediaPlayer [英] Should Singleton pattern be used for Android MediaPlayer

查看:482
本文介绍了单例模式是否应用于Android MediaPlayer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Android的新手,目前正在学习Android SDK.

I am a newbie to Android and currently learning the Android SDK.

我正在编写一个非常简单的应用程序,该应用程序在与小部件进行交互时将只播放各种声音.我正在使用MediaPlayer播放声音.

I am writing a very simple application that will just play various sounds upon interacting with widgets. I am using MediaPlayer to playback the sounds.

我的问题是我应该对MediaPlayer使用单例模式吗?我只希望共享MediaPlayer的一个对象,但是困惑来自以下链接

My question is should i use singleton pattern for MediaPlayer? I want just one object of MediaPlayer to be shared, but the confusion comes from the following link

https://developer.android.com/reference/android/media/MediaPlayer.html

其中说:

还建议不再使用MediaPlayer对象 在使用中,请立即调用release(),以便 与MediaPlayer对象关联的内部播放器引擎可以是 立即发布.资源可能包括单例资源,例如 硬件加速组件和无法调用release()可能 导致MediaPlayer对象的后续实例回退到 软件实施或完全失败.

It is also recommended that once a MediaPlayer object is no longer being used, call release() immediately so that resources used by the internal player engine associated with the MediaPlayer object can be released immediately. Resource may include singleton resources such as hardware acceleration components and failure to call release() may cause subsequent instances of MediaPlayer objects to fallback to software implementations or fail altogether.

我现在不想将MediaPlayer作为服务运行.

I don't want to run MediaPlayer as service for now.

那我该怎么办?我应该为MediaPlayer使用Singleton模式吗?

So what should i do? Should I use the Singleton Pattern for MediaPlayer?

非常感谢您的帮助.

推荐答案

从摘要中可以看出,建议不要将MediaPlayer对象保持为单例.主要是因为只要您将媒体播放器对象置于除END状态以外的任何状态,就可以锁定诸如硬件之类的宝贵资源.同样,当您调用release()时,无法重用该对象.您将需要一个新实例. 我认为android团队希望开发人员在需要时创建对象,并在完成后将其释放.

It is not advisable , as you can see from the extract, to keep an object of MediaPlayer as a singleton. Mainly because valuable resources such as hardware could be kept locked as long as you have a media player object in any of the states other than the END state. Also when you call release(), the object cannot be reused. You would need a new instance. I think the android team wants the developers to create objects when needed and release it when done.

这篇关于单例模式是否应用于Android MediaPlayer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆