使用的MediaPlayer播放同一文件中多次使用重叠 [英] Using MediaPlayer to play the same file multiple times with overlap

查看:1677
本文介绍了使用的MediaPlayer播放同一文件中多次使用重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是目前发生的事情与我的Andr​​oid应用程序:

我映射了一个简单的图像按钮,并让它播放声音上的点击。在每一次点击,我创建一个在我的原始文件夹中的声音文件中的MediaPlayer对象,我设置一个OnClickListener为MediaPlayer的对象,停止播放该文件,并释放它,然后我打了MediaPlayer对象。

I mapped a simple image to a button and have it play a sound on click. On each click, I create a MediaPlayer object with a sound file in my raw folder, I set an OnClickListener for that MediaPlayer object which stops playing the file and releases it, and then I play the MediaPlayer object.

的$ C $下的定义部分:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_activity);
    ImageButton start = (ImageButton) findViewById(R.id.imageButton1);
    start.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            MediaPlayer play = MediaPlayer.create(MainActivity.this, R.raw.dvno);
            play.setOnCompletionListener(new OnCompletionListener() {
                public void onCompletion(MediaPlayer mp) {
                    mp.stop();
                    mp.release();
                    mp = null;
                }
            });
            play.start();
        }
    });
}

什么是错的:

它工作正常,并不会崩溃,但它的可怕记忆和一般的速度很慢。我想,供用户点击按钮多次在一排,以最快的速度,他们可能可以,并听到声音立即播放一遍又一遍地与重叠。创建于每次点击一个新的MediaPlayer对象,并等待它完成并释放占用太多的资源,声音往往滞后于按钮的实际preSS。我希望能够创建一个单一的声音MediaPlayer对象,可同时对自身重叠播放。

It works fine and does not crash, but it's terrible for memory and very slow in general. I'd like for users to click the button many times in a row, as fast as they possibly can, and hear the sound play instantaneously over and over with overlap. Creating a new MediaPlayer object on each click and waiting for it to finish and be released consumes too many resources and the sound often lags behind the actual press of the button. I'd like to be able to create a single sound as MediaPlayer object which can be played while overlapping on itself.

可能的解决方案:

创建在的onCreate而不是的onClick范围单一的最终MediaPlayer对象并以某种方式使用线程启动MediaPlayer对象在每一次点击。我看,这可能是一个可行的解决方案,但我还没有使用过线程,我不知道他们是缓慢的,如果不慢,比我目前的code,所以我想知道如果有不使用螺纹的解决方案,它可能是更简单的。操纵单一的MediaPlayer,以自身重叠的状态,在这一点上似乎是不可能的,但也许我错了。

Create a single final MediaPlayer object in the scope of onCreate rather than onClick and somehow use threading to start the MediaPlayer object on every click. I read that this might be a possible solution, but I haven't ever used threads before and I don't know if they're slow, if not slower, than my current code, so I'd like to know if there's a solution without using threads that might be simpler. Manipulating the state of a single MediaPlayer to overlap on itself seems impossible at this point, but maybe I'm wrong.

这会崩溃,因为非法状态的程序?如果没有,这是怎么回事要慢一些,比我想它是什么?如果没有,任何人都可以提出一个修正我的code?

Would this crash the program because of illegal states? If not, is this going to be slower than I want it to be? And if not, can anyone suggest a fix to my code?

推荐答案

我建议你使用的Soundpool代替MediaPlayer的这一点。 教程是这里

I suggest you use a SoundPool instead of MediaPlayer for this. Tutorial is here

这篇关于使用的MediaPlayer播放同一文件中多次使用重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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