简体   繁体   English

Android停止播放媒体

[英]Android stops mediaplayer audio

I have a "click" that is high frequency, but after 20-25 clicks the sound disappear.. 我有一个高频率的“喀哒”声,但是单击20-25次后声音消失了。

 MediaPlayer mp;

                mp = MediaPlayer.create(mService.getApplicationContext(),R.raw.click2);
                mp.start();
            }

ANdroid blocks by Default this kind of loops? ANdroid默认会阻止这种循环吗?

Well I don't see a loop in the code you've provided, but if you actually are creating MediaPlayer s in a loop and not calling release() , then you are going to cause errors by using up too much of the available resources. 好吧,我没有在您提供的代码中看到一个循环,但是如果您实际上是在循环中创建MediaPlayer ,而不是调用release() ,那么您将因使用过多可用资源而导致错误。 I have several tips for you. 我有几个秘诀给你。

Tip #1: Don't use mService.getApplicationContext() , since (I assume) mService is a Service and already is a Context ( Service inherits from Context ). 提示1:不要使用mService.getApplicationContext() ,因为(我假设) mServiceService且已经 ContextService继承自Context )。 You should basically never use getApplicationContext() . 您基本上应该永远不要使用getApplicationContext()

Tip #2: Use one MediaPlayer and pay attention to the state machine . 提示2:使用一个MediaPlayer并注意状态机 Try something like this: 尝试这样的事情:

MediaPlayer mp = new MediaPlayer();
...
while (whateverCondition) { // I don't know what loop construct you are using...
    mp.reset();
    mp.setDataSource(this,
        Uri.parse("android.resource://com.your.package/" + R.raw.click2);
    mp.prepare();
    mp.start();
}
...
mp.release();

You will need to fill in the package name as appropriate, but it should get you on the right track. 您将需要适当地填写软件包名称,但这应该使您走上正确的道路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM